php - codeigniter ajax form_validation (with jquery) -
i trying validate fields witj ajax through codeigniter, can't quite figure out how it "right".
my ajax:
var timeout = null; $(document).ready(function(){ $('.new-user-box input').each(function(){ var key = $(this).attr('name'); $(this).on("keyup", function() { var value = $(this).val(); if(value=="") { return false; } var json = {}; json[key] = value; json['ajax'] = '1'; if (timeout) { cleartimeout(timeout); } timeout = settimeout( function() { $.ajax({ url: 'auth/ajax_validate', type: 'post', data: json, success: function(data) { console.log(data); } }) }, 1000) }); }) })
this lets input fields send value on keyup (after 1 second).
my php (just snippet username test):
<?php function ajax_validate() { // test if method called ajax , validate input field if($this->input->post('ajax')) { if($this->input->post('username')) { if($this->form_validation->set_rules('username', 'brugernavn', 'required|trim|min_length[1]|max_length[20]|is_unique[users.username]|xss_clean') && !$this->form_validation->run()) { $validates = 0; } else { $validates = 1; $error = ""; } $response = array($validates,$form_error('username')); echo json_encode($response); exit; } } } ?>
the response receive php error:
message: undefined variable: form_error
fatal error: function name must string in \path codeigniter\application\modules\auth\controllers\auth.php on line 401
hope has clue how fix this, or way. thankyou in advance.
take out $ before "form_error".
$response = array($validates,$form_error('username'));
to
$response = array($validates,form_error('username'));
Comments
Post a Comment