我正在尝试通过codeigniter验证一些ajax字段,但不太清楚如何“正确”。
我的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)
});
})
})
这基本上让我的所有输入字段在keyup上发送它们的值(在1秒后)。
我的php (只是用户名测试的一小段):
<?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;
}
}
}
?>
我收到的响应是一个php错误:
消息:未定义变量: form_error
致命错误:函数名必须是第401行上CODEIGNITER\application\modules\auth\controllers\auth.php的\路径中的字符串
希望有人知道如何解决这个问题,或者用另一种方法。提前谢谢你。
发布于 2013-04-05 17:51:31
去掉"form_error“前的$。
$response = array($validates,$form_error('username'));
至
$response = array($validates,form_error('username'));
发布于 2013-10-23 00:27:49
我发现了这个:
$this->input->is_ajax_request();
从这里开始:
Way to tell if a post came from an ajax call in codeigniter?
https://stackoverflow.com/questions/15840243
复制相似问题