codeigniter和表单提交

时间:2016-09-01 15:00:05

标签: php forms codeigniter

我正在尝试习惯Codeigniter。我很抱歉,如果这是一个微不足道或愚蠢的问题,但我一直在努力争取#34;新闻组" Codeigniter的教程工作。

有这种形式(来自here

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

,根据this控制器:

<?php
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'] = 'My News archive';

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

        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/', $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');
            }
        }
}
我认为,

如果验证返回正常,请继续将数据插入数据库。现在,我的问题是页面运行在:

http://localhost/codeigniter/index.php/news/

然而,提交按钮会将我返回到:

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

routes.php文件包含以下代码:

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

我不知道为什么会这样。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您是否在application / config / config.php中设置了base_url配置?