【问题标题】:codeigniter and form submitcodeigniter 和表单提交
【发布时间】:2017-01-09 12:59:10
【问题描述】:

我正在尝试习惯 Codeigniter。如果这是一个微不足道或愚蠢的问题,我很抱歉,但我一直在努力让 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';

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

【问题讨论】:

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

标签: php forms codeigniter


【解决方案1】:

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

【讨论】:

  • 非常感谢您的回复。在我的 config.php 中有这一行: $config['base_url'] = 'localhost/codeigniter';应该设置 base_url。
  • 会是这个吗?哇!!谢谢!我现在正在尝试您的建议!
  • 似乎不起作用。你能不能给我指点我可以看看的地方?你认为为什么会出现这个错误?谢谢!
  • 必须正确设置`$config['base_url']`。 form_open() 以及其他几个 CI 函数依赖于这个值。如果未设置,您最终会得到与表单提交一样的古怪网址。尝试包含这样的协议$config['base_url'] = 'http://localhost/codeigniter/;
  • @DFriend 所说的...您需要在base_url$config['base_url'] = 'http://localhost/codeigniter/; 上包含协议
猜你喜欢
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多