Question

I download CodeIgniter 2.1.0 and I followed the tutorial on CodeIgniter 2.1.0.

The problem is when I try to submit a form in this URL:

http://localhost/CodeIgniter/index.php/news/create

the page is redirected to this URL:

http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create

I tired many ways to change the route.php but it doesn't work. How can I fix this??

route.php

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

This is the code for the form page:

public function create()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['title'] = 'Create a news item';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('text', 'text', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header', $data);   
        $this->load->view('news/create');
        $this->load->view('templates/footer');

    }
    else
    {
        $this->news_model->set_news();
        $this->load->view('news/success');
    }
}

I notice that when the form page when the page is loaded, below code segment will executed, however, the else segment never got changes to execute.

if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header', $data);   
        $this->load->view('news/create');
        $this->load->view('templates/footer');

    }

This is the form page, nothing special, just call the create function shown above. create.php

<h2>Create a news item</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>

This is the HTML for the form page:

<html>
<head>
<title>Create a news item - CodeIgniter 2 Tutorial</title>
</head>
<body>
<h1>CodeIgniter 2 tutorial</h1><h2>Create a news item</h2>

<form action="localhost/CodeIgniter/index.php/news/create" method="post" accept-charset="utf-8">
<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><strong>&copy;2012</strong>
</body>
</html>

And when I press the create new item button, the URL changed to

http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create

and page shows 404 Pages Not Found

Was it helpful?

Solution

I found the solution, it caused by the improper base url setting.

Before my base url was: $config['base_url'] = 'localhost/CodeIgniter';

Now I changed it to

$config['base_url'] = 'http://localhost/CodeIgniter/';

and it works fine now.

Thanks for whoever tried to help:)

OTHER TIPS

Just a suggestion, may be it can helps you:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

Set this in your config.php file, you will never be worried about $config['base_url']. Because it will set http or https port, domain, port number (ex. 8080, 8880 etc.) and codeigniter root folder. Also it can be reused in future in your any codeigniter project. :)

you can also check this article

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top