Jump to content

[SOLVED] Problem on compare variable inside if ( variable =="a value" )


immunity

Recommended Posts

I try to craft Ajax. I got a  .php file which it gives $sInfo variable  values 0 (if no rows returned ) or 1 (if one or more rows returned)

with javascript i get this echo on var results.

 

I want to create  if (results ==0 ){ give element value ="not exists" }  if(results ==1) {give element value ="exists"}

 

but compiler doesnt go inside one of these 2 if :(

 

I checked without if and element get value = result (display in broswer 0  or 1)  how i can compare results ?

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

<html> 
<head> 
    <title>Customer Account Information</title> 
    <script type="text/javascript"> 
        var url = "GetCustomerData.php?id="; // The server-side script 
       function handleHttpResponse() {    
        if (http.readyState == 4) { 
              if(http.status==200) { 
                  var results=http.responseText; 
		if(results==1)
		{
			results ="exists";
		document.getElementById('divCustomerInfo').innerHTML = results;
		}
		if (results==0)
		{
		 	results ="not exist";
		document.getElementById('divCustomerInfo').innerHTML = results;
		}
              } 
              } 
        } 
        
        function requestCustomerInfo() {      
            var sId = document.getElementById("txtCustomerId").value; 
            http.open("GET", url + escape(sId), true); 
            http.onreadystatechange = handleHttpResponse; 
            http.send(null); 
        } 
function getHTTPObject() { 
  var xmlhttp; 

  if(window.XMLHttpRequest){ 
    xmlhttp = new XMLHttpRequest(); 
  } 
  else if (window.ActiveXObject){ 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    if (!xmlhttp){ 
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
    
} 
  return xmlhttp; 

  
} 
var http = getHTTPObject(); // We create the HTTP Object 
</script> 
</head> 
<body> 
    <p>Enter customer ID number to retrieve information:</p> 
    <p>Customer ID: <input type="text" id="txtCustomerId" value="" /></p> 
    <p><input type="button" value="Get Customer Info" onclick="requestCustomerInfo()" /></p> 
    <div id="divCustomerInfo"></div> 
</body> 
</html>

Link to comment
Share on other sites

add an alert(results); before you compare to see what the actuall value of results.

 

Also, results is a string, so your comparisson should be

 

if (results == "1")

 

with quotes around the one

 

finally, you can use

 

if (results == "1") { ... }

else { ... }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.