Welcome Guest, Not a member yet? Register   Sign In
Form validation and redirects
#1

[eluser]ToddyBoy[/eluser]
I have a problem with form validation and how to take the user to where I want them to go with appropriate messages included.

I've used something like this
Code:
function send_request()
    {        
        $this->load->library('form_validation');
        
        // form rules go here
        
        if ($this->form_validation->run() == FALSE){
            $data['message'] = 'error';
            $this->load->view('view_name', $data);
        } else {
            $data['message'] = 'success';
            $this->load->view('view_name', $data);
        }
    }
to get an error message. That part is fine. I can get set_value() to work as it should.

The thing I would like to know is if I can change the loading of a view to a redirect (to the same page) if the validation fails.

Here is what I mean. Say my original URL is www.mysite.com/page/another-page. This location has a form in it which submits to /page/send_request. If the form validation fails, the load view shows the original page under the URL of /page/send_request. I don't want the user to see that /page/send_request location.

I would like to try something like
Code:
function send_request()
    {        
        $this->load->library('form_validation');
        
        // form rules go here
        
        if ($this->form_validation->run() == FALSE){
            redirect('/page/another-page/error');
        } else {
            redirect('/page/another-page/success');
        }
    }
The above code works fine if the form validates correctly. I don't care about the form contents not being there. However, if the form fails validation, the form contents are not displayed, even with set_value().

I realise that a redirect clears form post data. Is there a way to somehow keep the form data and still use a redirect? I'm thinking that a session might be the answer but I would rather avoid creating and destroying sessions.


Messages In This Thread
Form validation and redirects - by El Forum - 10-30-2010, 12:54 PM
Form validation and redirects - by El Forum - 11-01-2010, 10:32 AM
Form validation and redirects - by El Forum - 11-01-2010, 10:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB