xThunder xThunder - 1 year ago 65
PHP Question

PHP CodeIgniter load view issue, loading full path twice

I'm new to PHP CodeIgniter and I'm creating my first app, I've created the controller, model and views to add records to my DB and so far so good,

The problem starts when I click 'submit' in my form,

as instead of redirecting to the same form again if there is an issue in the form or redirecting to the success page, it redirects to the same page but with the full path twice,

like this:

form page - localhost/index.php/news/create

expected results:

form data is valid ->localhost/index.php/news/success

form is invalid -> localhost/index.php/news/create (same page)

but instead it does this when I click submit:

localhost/index.php/news/localhost/index.php/news/create

as you can see, it takes the full path again and puts it afterwards the already existing url.

this is my code

routes.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';
$route['news/create'] = 'news/create';


main controller

public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}


controller that has the function create of the form

class News extends CI_Controller {
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');
}
}
}


model:

public function set_news()
{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);

$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);

return $this->db->insert('news', $data);
}


Form

<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>

Answer Source

The problem was in config.php

I replaced

$config['base_url'] = 'localhost'; 

to this:

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

and everything is working properly now

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download