I am trying to store this.value user wrote in eMailTxt box in a Global Variable and then let AJAX do the checking of User exists or not.
我想把它存起来。value user在全局变量的eMailTxt框中写入,然后让AJAX检查用户是否存在。
HTML
Passing value onChange to ValidUser
将值onChange传递给ValidUser
<input type="text" name="eMailTxt" id="eMailTxt" onChange="ValidUser(this.value)" />
AJAX##
Checks if value exists in database through php
通过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();
}
4 个解决方案
#1
1
Add this before your function
like,
在你的函数之前加上这个,
var myStr=''
function ValidUser(str)
{
myStr=str; //Add this for assigning previous value in myStr variable
....
Also , if you are using ajax
then use $.ajax function
for this,
此外,如果您正在使用ajax,那么请使用$。ajax函数,
var myStr='';
function ValidUser(str)
{
myStr=str;
$.ajax({
url:'loginVerify.php',
type:'GET',
data:{q:str},
success:function(response){
$("#eMailTxt").css({'background-color':'#fed3da','color':'red'});
$("#eMailTxt").val("[User Does Not Exist]");
}
});
}
#2
1
Try this:
试试这个:
var eMailTxtVal = '';
function ValidUser(str)
{
eMailTxtVal = str;
$.get("loginVerify.php?q=" + str, function(data) {
if (data == "false") {
$("#eMailTxt").css({'background-color': '#fed3da', 'color': 'red'});
$("#eMailTxt").val("[User Does Not Exist]"); //Changes text if user does not exist
}
else {
$("#eMailTxt").css({'background-color': '#d3fef3'});
}
});
}
$("#eMailTxt").focus(function() {
$(this).val(eMailTxtVal);
});
#3
1
you might need this thing. not mentioning var will make it global
你可能需要这个东西。不提及var将使它成为全球性的
myStr='';
ValidUser = function(str)
{
myStr= str;
}
#4
0
Just declare the variable "above" the scope of any of your functions. Set it before you change it and then recall it when you need it.
只要将变量“上面”声明为函数的范围即可。在更改它之前设置它,然后在需要时回忆它。
#1
1
Add this before your function
like,
在你的函数之前加上这个,
var myStr=''
function ValidUser(str)
{
myStr=str; //Add this for assigning previous value in myStr variable
....
Also , if you are using ajax
then use $.ajax function
for this,
此外,如果您正在使用ajax,那么请使用$。ajax函数,
var myStr='';
function ValidUser(str)
{
myStr=str;
$.ajax({
url:'loginVerify.php',
type:'GET',
data:{q:str},
success:function(response){
$("#eMailTxt").css({'background-color':'#fed3da','color':'red'});
$("#eMailTxt").val("[User Does Not Exist]");
}
});
}
#2
1
Try this:
试试这个:
var eMailTxtVal = '';
function ValidUser(str)
{
eMailTxtVal = str;
$.get("loginVerify.php?q=" + str, function(data) {
if (data == "false") {
$("#eMailTxt").css({'background-color': '#fed3da', 'color': 'red'});
$("#eMailTxt").val("[User Does Not Exist]"); //Changes text if user does not exist
}
else {
$("#eMailTxt").css({'background-color': '#d3fef3'});
}
});
}
$("#eMailTxt").focus(function() {
$(this).val(eMailTxtVal);
});
#3
1
you might need this thing. not mentioning var will make it global
你可能需要这个东西。不提及var将使它成为全球性的
myStr='';
ValidUser = function(str)
{
myStr= str;
}
#4
0
Just declare the variable "above" the scope of any of your functions. Set it before you change it and then recall it when you need it.
只要将变量“上面”声明为函数的范围即可。在更改它之前设置它,然后在需要时回忆它。