I am trying to validate some fields witj ajax through codeigniter, but can't quite figure out how to it 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 basically lets all my input fields send their value on keyup (after 1 second).
My php (just a snippet from the username test):
<?php
function ajax_validate()
{
// Test if the method is called by ajax and validate the 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 i receive is a php error:
Message: Undefined variable: form_error
Fatal error: Function name must be a string in \PATH TO CODEIGNITER\application\modules\auth\controllers\auth.php on line 401
Hope someone has a clue how to fix this, or do it another way. Thankyou in advance.