1
votes

Okay, so I just started learning CodeIgniter last night and I'm just about to finish the final part of the tutorial today, however, i get a 404 page not found error whenever i click the submit button.

Here is the last part of the tutorial where it tells you to create a form and store the data to the database: http://www.codeigniter.com/user_guide/tutorial/create_news_items.html

My URL for the form is http://localhost/ci/index.php/news/create

but then when I clicked the submit button, the URL turns to http://localhost/ci/index.php/news/localhost/ci/index.php/news/create

enter image description here

weird, i know theres a problem with the routing but I don't know the specific ones.

Here's my Route file:

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

Controller:

class News extends CI_Controller{
public function __construct(){
    parent::__construct();
    $this->load->model('news_model');
    $this->load->helper('url_helper');
}

public function index(){
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';

    $this->load->view('templates/header',$data);
    $this->load->view('news/index',$data);
    $this->load->view('templates/footer',$data);
}

public function view($slug = NULL){
    $data['news_item'] = $this->news_model->get_news($slug);

    if(empty($data['news_item'])){
        show_404();
    }

    $data['title'] = $data['news_item']['title'];

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

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:

class News_model extends CI_Model{
public function __construct(){
    $this->load->database();
}

public function get_news($slug = FALSE){
    if($slug === FALSE){
        $query = $this->db->get('news');
        return $query->result_array();
    }

    $query = $this->db->get_where('news',array('slug'=>$slug));
    return $query->row_array();
}

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:

enter image description here

1
<form action=""></form> tag is missing. Check it. - Md Mahfuzur Rahman
please reload the page, i changed the snippet to image.. thanks - flix
Try changing form_open('news/create'); to form_open(base_url('news/create'));. - Sankar V
still doesn't work :( - flix
Have you set base url on your config file? - otezz

1 Answers

0
votes

Solution:

  1. open application\config\config.php
  2. look for $config['base_url']
  3. set it to $config['base_url'] = '';