Welcome Guest, Not a member yet? Register   Sign In
Form validation
#2

[eluser]cideveloper[/eluser]
First of all dont use $_POST, use $this->input->post
Secondly in either case you are just redirecting to

Code:
'blog/comments/'.$_POST['entry_id']

thus in both instances you will get the same result

you can do something like this even though I dont think its the best way.

Code:
function comment_insert() {

    $this->load->library('form_validation');
    $this->form_validation->set_rules('body', 'Comment', 'required|xss_clean');
    $this->form_validation->set_rules('author', 'Name', 'trim|required|min_length[3]|max_length[15]|xss_clean');

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

        $data = array (
            'title' => 'My Comment Title',
            'heading' => 'My Comment Heading',
            'query' => $this->db->where('entry_id', $this->uri->segment(3))->get('comments'),
            'message' => (validation_errors()) ? validation_errors() : ''
        );
        $this->session->set_flashdata('message', $data['message']);
        redirect('blog/comments/'.$this->input->post('entry_id'));

    } else {
        
        $data = array (
            'body' => $this->input->post('body'),
            'author' => $this->input->post('author'),
        );
        $this->db->insert('comments', $data);
        $this->session->set_flashdata('message', 'Comment Added');
        redirect('blog/comments/'.$this->input->post('entry_id'));

    }

}

and in the view you would have

Code:
<?php echo $this->session->flashdata('message'); ?>


Like I said, I dont think this is the best way. usually you should have the form being displayed using load view when the form_validation is false.


Messages In This Thread
Form validation - by El Forum - 06-06-2011, 08:31 AM
Form validation - by El Forum - 06-06-2011, 11:14 AM
Form validation - by El Forum - 06-06-2011, 01:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB