次の ajax スクリプトが 1 を返さない場合、validator.registercallback 関数全体に対して true を返そうとしています。それを理解してください。
validator.registerCallback('unique_username', function(value) {
//use ajax to run the check
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if(xmlhttp.responseText != 1) {
alert('Username Exists');
return false;
} else {
alert('Username Available!');
return true;
}
}
}
xmlhttp.open("GET","uniqueuser.php?username="+value,true);
xmlhttp.send();
})
奇妙なのは、次のように機能することです。ajax スクリプトの値に基づいて機能しないだけです。
validator.registerCallback('unique_username', function(value) {
//use ajax to run the check
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if(xmlhttp.responseText != 1) {
alert('Username Exists');
return false;
} else {
alert('Username Available!');
return true;
}
}
}
xmlhttp.open("GET","uniqueuser.php?username="+value,true);
xmlhttp.send();
return true;
})
したがって、基本的には、メイン関数で true を返すように指示できますが、ajax スクリプトの値が 1 を返さない場合にのみ true を返そうとすると、機能しません。ちなみに、アラートは機能します。したがって、正しい値を取得していますが、trueまたはfalseを返すことはありません。
どんな助けでも大歓迎です!
アップデート
validator.registerCallback('unique_username', function(value) {
//use ajax to run the check
function ajax_result() {
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
}
}
xmlhttp.open("GET","uniqueuser.php?username="+value,true);
xmlhttp.send();
}
if(ajax_result() != 1) {
alert('Username Exists');
return false;
} else {
alert('Username Available!');
return true;
}
})