我有一个表单供用户输入一些反馈,此表单必须存放在产品详细信息页面中。我需要在详细信息页面上打印出一些错误验证,而不是将表单重定向到带有验证消息的反馈表单页面。
产品详细信息页面位于'index.php / product / view / 1',而反馈表单位于'index.php / product / add_feedback'。
如何打印出错误表单验证消息,以便它显示在产品详细信息页面上,而不是重定向到add_feedback。谢谢。
我的控制器:
class Product extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('mproduct');
$this->load->model('mfeedback');
}
public function index()
{
//get product details
$data['content'] = $this->mproduct->get_details();
$this->load->view('listing', $data);
}
public function add_feedback()
{
// feedback form
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean|max_length[200]');
$this->form_validation->set_rules('feedback', 'Feedback', 'required|xss_clean|max_length[200]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('feedback');
}
else
{
$pid = $this->input->post('pid');
$name = $this->input->post('name');
$feedback = $this->input->post('feedback');
$this->MFeedback->add($pid, $name, $feedback);
redirect('product/view/'.$pid);
}
}
}
型号:
class MFeedback extends CI_Model {
function add_feedback($name, $pid, $feedback)
{
$data = array(
'name' => $name,
'feedback' => $feedback,
'pid' => $pid,
);
$this->db->insert('feedback', $data);
}
}
查看 - feedback.php
<h1>Add Feedback</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('product/add_feedback'); ?>
<p>Name</p>
<input type="text" name="name" size="50" value="<?php echo set_value('name'); ?>" />
<p>Feedback</p>
<textarea type="text" name="feedback"><?php echo set_value('feedback'); ?></textarea>
<?php echo form_hidden('pid', $this->uri->segment(3, 0)); ?>
<div><input type="submit" value="Add Feedback" /></div>
</form>
答案 0 :(得分:2)
简单!只需将验证添加到Product/index
- 方法,如下所示:
class Product extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('mproduct');
$this->load->model('mfeedback');
}
public function index()
{
// feedback form validation
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean|max_length[200]');
$this->form_validation->set_rules('feedback', 'Feedback', 'required|xss_clean|max_length[200]');
if ($this->form_validation->run() == TRUE)
{
// the validation passed, lets use the form data!
$pid = $this->input->post('pid');
$name = $this->input->post('name');
$feedback = $this->input->post('feedback');
$this->MFeedback->add($pid, $name, $feedback);
redirect('form/success'); // redirect to a page, where the user gets a "thanks" message - or redirect to the product page, and show a thanks there (but be sure to use redirect and nocht $this->load->view(..), because then the form data would be still in the header and a reload of the page would send another mail :)
}
// form did not pass the validation, lets get and show the product details
$data['content'] = $this->mproduct->get_details();
$this->load->view('listing', $data);
}
}
在文件feedback.php
中,您必须将表单目标更改为以下内容:
<?php echo form_open('product/'.$this->uri->segment(3, 0)); ?>
......甚至更好:
<?php echo form_open('product/'.$content->id); ?>
...取决于您的产品视图。