Welcome Guest, Not a member yet? Register   Sign In
JQuey & Form Validation
#1

[eluser]jstine[/eluser]
Hi all,

So, I'm having a problem retrieving CI error messages to my form that I'm posting via JQuery Ajax.

Basically, I use a JQuery Ajax post to post the data to my function. I then run the validation and if it returns false I try to echo the errors with

Code:
echo validation_errors();

In my ajax function I have the success set to take the response and present it to the user via a alert box, but even when there are errors the alert box just pops up with no error information. If I change my "echo validation_errors();" to "echo "hello";" the alert displays the "hello" as expected when there are errors.

Does anyone have any ideas why echo validation_errors(); doesn't work here and what I can do to fix it? I've been pulling my hair out!

Thanks in advance.
#2

[eluser]Twisted1919[/eluser]
You need to return the errors from within your controller, not echo them in php view file.
so you will have something like:
Code:
//PHP class method
public function validate(){

  [...]
  if($this->form_validation->run()==false)
  {
     exit( json_encode( array('result'=>'error','msg'=>validation_errors() ) ) );
  }
  [...]

}
//Next, your js code will be like:
$('#form').submit(function(){
  var formData=$(this).serialize();
  $.ajax({
      [...]
       data:formData,
       dataType:'json',
       success:function(object){
          if(object.result=='error'){
             alert(object.msg);//shows the validation errors.
          }
       }
      [...]
  });

  return false;
});
#3

[eluser]jstine[/eluser]
Thanks for the reply. For some reason I still can't get it to work.

Here's my controller code:

Code:
$this->form_validation->set_rules('su_first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('su_last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('su_user_email', 'Email', 'trim|required|valid_email|callback__check_su_user_email');
        $this->form_validation->set_rules('su_user_pass', 'Password', 'required|matches[su_confirm_pass]');
        $this->form_validation->set_rules('su_confirm_pass', 'Confirm Password', 'required');
        if($this->form_validation->run() == false)
        {
            exit( json_encode( array('result'=>'error','msg'=>validation_errors() ) ) );
}

And my view's js:
Code:
var user_first = $("input#su_first_name").val();
    var user_last = $("input#su_last_name").val();
    var user_email = $("su_user_email").val();
    var user_pass = $("su_user_pass").val();
    var user_confirm = $("su_confirm_pass").val();
    $.ajax({
          type: "POST",
          url: "myurl",
          data: ({'su_first_name':user_first,'su_last_name':user_last,'su_user_email':user_email,'su_user_pass':user_pass,'su_confirm_pass':user_confirm}),
          dataType:'json',
          success: function(object) {
                if(object.result=='error'){
                    alert(object.msg);//shows the validation errors.
            }
          }
        });

Any ideas why it's still not working are appreciated!
#4

[eluser]Twisted1919[/eluser]
Using firebug, can you see the result of the request ? (if you don't have firebug, install it, it will help you allot, it is a firefox extension that should't miss to any developer)
Also, can you post the full JS code for form submission ?
#5

[eluser]jstine[/eluser]
Thanks for helping with this.

Using firebug the response I get from the post is as follows:

{"result":"error","msg":""}

It looks like no error message is being generated.

My javascript looks like this:
Code:
function createAccount()
{
    var user_first = $("input#su_first_name").val();
    var user_last = $("input#su_last_name").val();
    var user_email = $("input#su_user_email").val();
    var user_pass = $("input#su_user_pass").val();
    var user_confirm = $("input#su_confirm_pass").val();
    $.ajax({
          type: "POST",
          url: "<?=base_url()?>my_codeigniter_fuction",
          data: ({'su_first_name':user_first,'su_last_name':user_last,'su_user_email':user_email,'su_user_pass':user_pass,'su_confirm_pass':user_confirm}),
          dataType:'json',
          success: function(object) {
                if(object.result=='error'){
                    alert(object.msg);//shows the validation errors.
            }
          }
        });
}




Theme © iAndrew 2016 - Forum software by © MyBB