【问题标题】:Codeigniter update form does not submitCodeigniter 更新表单未提交
【发布时间】:2017-05-21 14:24:39
【问题描述】:

Codeigniter 为您提供了解它以及如何使用它的教程。本教程教您在数据库的表中创建元素,但不教您如何更新这些元素。我正在尝试使用 codeigniter 库进行更新查询,但来自更新视图的表单未提交。

在 NEWS_MODEL:

public function update_news($slug)
			{

			$this->load->helper('url');

			$data = array(
			               'title' => $this->input->post('title'),
			               'slug' => $slug,
			               'text' => $this->input->post('text')
			            );

			$this->db->where('slug', $slug);
			$this->db->update('news', $data); 

			}

在新闻控制器处:

public function update($slug)
        {

            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['title'] = 'Update 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)
            {
              // The form was NOT posted
              // So we load the view
              $data['news_item'] = $this->news_model->get_news($slug);
              $this->load->view('templates/header', $data);
              $this->load->view('news/update', $data);
              $this->load->view('templates/footer');

            }
            else
            {
              // The form was submitted
              $data = array(
           'title' => $this->input->post('title'), //-->post variable
           'slug'  => $this->input->post('slug'),
           'text' => $this->input->post('text')
                );

              $this->news_model->update_news($slug, $data);
              $this->load->view('news/success');
            }
          }

在更新视图中:

<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php  echo form_open('news/update') ;?>

<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="Update news item" />

</form>

我用“console.log()”测试了代码,问题似乎是更新视图的表单没有提交,但我不知道为什么,因为表单类似于创建表单,这确实工作得很好:

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

【问题讨论】:

    标签: javascript php mysql forms codeigniter


    【解决方案1】:

    你的问题是: 你在这一行使用=== 而不是==:

     if ($this->form_validation->run() === FALSE)
    

    【讨论】:

    • 在 Codeigniter 示例代码中是这样的,反正我只是试图改变它,它仍然不起作用。
    • 错误是“未找到404”,但我确定这是因为更新视图中的表单,我只是不知道如何解决。
    • 我认为问题出在你的 config.php 文件中,你确定,你设置了$config['base_url']??
    • $config['base_url'] 已设置。它已经在 Codeigniter 的 config.php 中设置(我刚刚检查过)
    • 在 我不得不使用 (发送参数, 如你所说)。我从这样的 url 得到 $slug:
    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 2012-06-16
    • 2017-10-08
    相关资源
    最近更新 更多