我试图通过codeigniter来验证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)
});
})
})
Run Code Online (Sandbox Code Playgroud)
这基本上让我的所有输入字段在keyup上发送它们的值(1秒后).
我的php(只是用户名测试的一个片段):
<?php
function ajax_validate()
{
// Test if the method is called by ajax and validate …Run Code Online (Sandbox Code Playgroud)