我试图将 this.value 用户写入 eMailTxt 框中的全局变量中存储,然后让 AJAX 检查用户是否存在。
HTML
将值 onChange 传递给 ValidUser
<input type="text" name="eMailTxt" id="eMailTxt" onChange="ValidUser(this.value)" />
AJAX##
通过php检查数据库中是否存在值
function ValidUser(str)
{
//Want to declare a global variable here
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (xmlhttp.responseText=="false")
{
$("#eMailTxt").css({'background-color':'#fed3da','color':'red'});
$("#eMailTxt").val("[User Does Not Exist]"); //Changes text if user does not exist
//Want to store str in global variable so that in $("#eMailTxt").focus function I can store back what was written earlier
}
else
{
$("#eMailTxt").css({'background-color':'#d3fef3'});
}
}
}
xmlhttp.open("GET","loginVerify.php?q="+str,true);
xmlhttp.send();
}