Contact me on

I'm specialized in programmable devices, Matlab, and recently moving to mobile applications.
More about me..
   

Saturday, 17 October 2015

Code Igniter form_open changes URL

 If you are a new user to the php framework CodeIgniter, you may face a problem when submitting a form using helper form_open(). Let's discuss by the following example which is downloaded with the framework:


Step 1: After downloading and installing the framework, we have the following project structure.
|- system/
|- application/
|---- models/
|---- views/
|---- controllers/
|- public/
|---- images/
|---- js/
|---- css/
|---- index.php
|---- .htaccess

Step 2: We have the form in the file views/news/create.php
<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>

    <label for="title">Title</label>
    <input type="input" name="title" /><br />

    <label for="text">Text</label>
    <textarea name="text"></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form>
Step 3: Complete the tutorial, and run from the localhost the url
../<localhost>/views/news/create
Step 4: For me the form did not submit the values to the database, instead a php error was displayed, and I noticed that the url changed
<!-- The url changed to -->

../<localhost>/views/news/create/application/news/create

Solution: The solution is straightforward, we must set base_url properly inside /application/config.php
/* ~/application/config.php */

$config['base_url'] = ' ';   // Either leave empty
// (or)
$config['base_url'] = '/www/drv/etc/ ';   // Enter full path where application folder exists
We have two options, either to leave it empty and let Code Igniter determine the absolute path, or specify the full path. According to documentation, it is preferred to use full path, e.g. ‘/www/MyUser/system’.



4 comments: