I have checked the return string is “added” or “used” or “false”, but every time it do the last statement which is document.write(theresult). What is the problem, i am expecting if the return string is “used” then alert “1”. Please help, many thanks.
function add_member()
{
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)
{
// document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
var theresult = xmlhttp.responseText;
if (theresult == "used") {
alert("1");
}
else if (theresult == "added") {
alert("2");
}
else if (theresult == "false") {
alert("3");
}
else
{
document.write(theresult);
}
}
}
xmlhttp.open("GET","add.php?addnew=true&email=" + document.memberform.email.value + "&tel=" + document.memberform.tel.value,true);
xmlhttp.send();
}
@JBL Ajax response text is not a plain string even though you set a string to it. You cannot compare it. If you want to pass a string and compare, do it in response header.