I have a form for user to input some feedbacks, and this form has to be resided in a product detail page. I am required to print out some error validation on the detail page instead of having the form redirected to the feedback form page with the validation message.
The product detail page is located at 'index.php/product/view/1', while the feedback form is at 'index.php/product/add_feedback'.
How can I print out the error form validation message so that it shows on the product detail page, instead of redirection to the add_feedback. Thank you.
My controller:
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);
}
}
}
Model:
class MFeedback extends CI_Model {
function add_feedback($name, $pid, $feedback)
{
$data = array(
'name' => $name,
'feedback' => $feedback,
'pid' => $pid,
);
$this->db->insert('feedback', $data);
}
}
view - 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>
$this->load->library('form_validation');inproduct/view?