gpt4 book ai didi

javascript - Codeigniter 更新表单未提交

转载 作者:行者123 更新时间:2023-11-29 19:37:21 26 4
gpt4 key购买 nike

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

AT 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');
}
}

更新 View :

<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()”测试了代码,问题似乎是更新 View 的表单没有提交,但我不明白为什么,因为该表单与创建表单类似,效果很好:

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

最佳答案

你的问题是:您在此行中使用 === 而不是 ==:

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

关于javascript - Codeigniter 更新表单未提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41512210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com