1
好的,所以我昨天晚上剛開始學習CodeIgniter,並且我即將完成本教程的最後部分,但是,每當我單擊提交時,我都會收到一個404頁面未找到錯誤按鈕。CodeIgniter教程表單提交出錯
這裏是它會告訴你創建一個表單和數據存儲到數據庫教程的最後一部分:http://www.codeigniter.com/user_guide/tutorial/create_news_items.html
我對形式的網址是http://localhost/ci/index.php/news/create
但後來當我點擊提交按鈕時,URL轉向http://localhost/ci/index.php/news/localhost/ci/index.php/news/create
奇怪,我知道那裏有與路由選擇的問題,但我不知道具體的。
這是我的路線文件:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
控制器:
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');
}
}}
型號:
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);
}}
形式:
'
'標記丟失。覈實。 –請重新加載頁面,我將片段更改爲圖片。謝謝 – flix
嘗試更改'form_open('news/create');'to'form_open(base_url('news/create'));'。 –