CodeIgniter Forums
Validation error with jquery(ajax) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Validation error with jquery(ajax) (/thread-47687.html)



Validation error with jquery(ajax) - El Forum - 12-18-2011

[eluser]Unknown[/eluser]
Hi all,

I am currently working on submitting a form and once it is submitted it will validate using CI then return it out using ajax. I have currently tried something but i am not having too much luck getting it to work. Can someone tell me what i'm doing wrong here:

JS

Code:
$(document).ready(function() {
  $('#regform').submit(function(){
   $.ajax({
    url: $(this).attr('action'),
    type: 'POST',
    data: $(this).serialize(),
    dataType: 'json',
    success: function(respond) {
     if (respond.result == 'false') {
      $('#feedback').html(respond.errors);
    
     } else {

     }
    }
   });
   return false;
  });
});


Controller
Code:
$this->load->library('form_validation');
  
   $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|xss_clean|callback_dbcheck');
   $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[20]|xss_clean');
   $this->form_validation->set_rules('email', 'E-mail', 'required|min_length[3]|max_length[30]|xss_clean|valid_email|callback_verify_email');
   $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[20]|xss_clean');
   $this->form_validation->set_rules('password_conf', 'Re-type Password', 'required|min_length[3]|max_length[20]|xss_clean|matches[password]');

   if ($this->form_validation->run() == FALSE) {
    $respond['result'] = 'false';
    $respond['errors'] = validation_errors();
    //$this->load->view('viewregister');
     echo json_encode($respond);
   }


The odd part is when i refresh the page (before submitting) all i see is this

{"result":"false","errors":""} I don't understand why i am only getting this instead of the forms which i should be viewing


any ideas?