Welcome Guest, Not a member yet? Register   Sign In
Form validation always returns false in all cases


I am quite new to Code Igniter.

I have a form like the one below. Whenever I post it to the controller and run the validation it is always in every case invalid even with both fields filled in or even without the rules defined at all.

The form is posted to a different url than the one on which the form is displayed. It is not an ajax post - I thought that might be the problem but it is now posted by the browser in response to the submit button being pressed.

<form action="http://domain/clients/admin/business/ajax_business" method="post" accept-charset="utf-8">
  <label for="first_name">First Name</label>
  &lt;input type="input" name="first_name"&gt;&lt;br>

  <label for="last_name">Last Name</label>
  &lt;input type="input" name="last_name"&gt;&lt;br>

  &lt;input type="submit" name="submit" value="Update"&gt;


public function ajax_business() {

        $this->load->helper(array('form', 'url'));

               $this->form_validation->set_rules('first_name', 'First Name', 'required');
               $this->form_validation->set_rules('last_name', 'Last Name', 'required');        
                $response = new stdClass();

                if ($this->form_validation->run() == FALSE)
//whatever i do this block is always run
                        $response->result = false;
                        $response->result = true;



--Justin Wyllie

everything looks right to me, Justin.

Put a var_dump($_POST); on your first line of the ajax_business() function and double check what is getting passed. It is possible there is something in your setup stripping the post values

&lt;form action="http://domain/clients/admin/business/ajax_business" method="post" accept-charset="utf-8"&gt;

Try :
replace the http://domain....../...../.../../ajax_business with just the path to your controller.

&lt;form action="business/ajax_business" method="post" accept-charset="utf-8"&gt;


They way you have defined the action path makes it look like you have the following stucturesetup.

business <-- this is your controller and your calling ajax_business method.

Unless your doing something funky. Or unless you have the main CI direcctory burry deep under your domain.

Just a thought.


Working now. No idea what the problem was.

Nope nothing funky. The root to my CI app is in /clients and then I have sub-folders in the controllers folder so /admin is just a folder under /controllers.

I think the problem may have been something like there was an error in my Ajax code so I did a POST but with no data and when I started submitting the form by a normal form submit it still sent no data. Something like that.

Theme © iAndrew 2016 - Forum software by © MyBB