2

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>
2
  • have you set $this->load->library('form_validation'); in product/view? Commented Feb 3, 2012 at 9:14
  • i set it at the autoload, so it did display, just that i need the error validation message to appear on within the product details page as well, instead of the form being redirect to the index.php/product/add_feedback Commented Feb 3, 2012 at 9:58

1 Answer 1

2

Simple! Just add the validation to Product/index-method, like this:

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

And in the file feedback.php you'll have to change the form target to something like this:

<?php echo form_open('product/'.$this->uri->segment(3, 0)); ?>

... or even better:

<?php echo form_open('product/'.$content->id); ?>

... depends on your product view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.