controller -
<?php
class News extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('News_model');
$this->load->helper('url_helper');
}
public function create(){
echo $data['title']=$data['title1'] = 'Form';
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
$this->load->view('news/create', $data);
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');
}
}
}
?>
view-
<h2><?php echo $title1; ?></h2>
<?php echo validation_errors();
$this->load->helper('form'); ?>
<?php echo form_open(); ?>
<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>
model-
<?php
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function getnews($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();
}
}
?>
遇到错误-在localhost上提交表单时找不到404页面。提交表单后链接看起来像这样-http://localhost/ci/index.php/news/localhost/ci/index.php/news/create
遇到错误-在localhost上提交表单时找不到404页面。提交表单后链接看起来像这样-http://localhost/ci/index.php/news/localhost/ci/index.php/news/create
答案 0 :(得分:0)
请按照以下步骤操作:
1。。您需要在自动加载文件中加载“ url_helper”,这是一种最佳做法,而不是在每个页面中编写。 路径(应用程序>配置> autoload.php)
$autoload['helper'] = array('url');
2。。您需要在视图页面的顶部添加以下行。
<base href="<?php echo base_url(); ?>">