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.
#2

[eluser]pickupman[/eluser]
You are correct, setting session variables would be the only way to carry to another URL. As a thought, how many users actually look a url? When you have filled out a form, have you ever paid attention to the url?
#3

[eluser]smilie[/eluser]
I know what you mean. There is a way around it, but it requires a lot more of code to be written and as pickupman said - if you do not care about URL too much I wouldn't bother.

Anyway, this is the way, so if you like it - use it Smile

Code:
function form($action='')
{
   if($action == '')
   {
      # show default (empty) form page
      $this->load->view('form');
   }
   elseif($action == 'submitted')
   {
      # user has submitted form, so do form checks
      if($this->form_validation->run() == FALSE)
      {
         $data['message'] = 'error';
         $this->load->view('form', $data);
      }
      else
      {
         $data['message'] = 'success';
         $this->load->view('form',$data);
      }
    else
    {
       # Just in case somebody is messing with the form (through i.e. firebug)
       $data['message'] = 'no can do';
       $this->load->view('form',$data);
    }
}

This way, you always stay on a same controller - ergo you always have same URL.
However, you will have to expand your 'form' view php file to accept $message and also to send extra hidden form 'submitted'. This way in your controller you can decide if form has been submitted - or do you need to display 'empty' form.

Hope this helps.

Regards,
Smilie




Theme © iAndrew 2016 - Forum software by © MyBB