Topic awaiting preservation: AJAX xmlhttp.responsetext and php (Page 1 of 1) |
|
---|---|
Bipolar (III) Inmate From: |
posted 09-17-2009 02:14
What i am trying to accomplish is to send an xmlhttp.request to a php page which has an array of name. if the request matches any name in the array it will return that name so i can test for equality. It works well with one name. my question is can i send back more than just one name.. like lets say a name and a message and check equality for the name and display the message. basically im asking can the xmlhttp.responsetext be used like an object with different properties so i can pull back different things to set as variables in my javascript. code: <html> <head> <script src="clienthint.js"></script> </head> <body> <form name="form1"> First Name: <input type="text" id="txt1" onkeyup="showHint(this.value)" /> </form> <p>Suggestions: <span id="txtHint"></span></p> </body> </html>
code: var xmlhttp function showHint(str){ if (str.length==0){ document.getElementById("txtHint").innerHTML=""; return; } xmlhttp=GetXmlHttpObject(); if (xmlhttp==null){ alert ("Your browser does not support XMLHTTP!"); return; } var url="gethint.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged(){ if (xmlhttp.readyState==4){ document.getElementById("txtHint").innerHTML=xmlhttp.responseText; if (document.getElementById("txt1").value == xmlhttp.responseText){ document.getElementById("txt1").style.backgroundColor = "#FFFF99"; } else { document.getElementById("txt1").style.backgroundColor = "#FFFFFF"; } } } function GetXmlHttpObject(){ if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject){ // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }
code: <?php // Fill up array with names $a[]="Anna"; $a[]="Brittany"; $a[]="Cinderella"; $a[]="Diana"; $a[]="Eva"; $a[]="Fiona"; $a[]="Gunda"; $a[]="Hege"; $a[]="Inga"; $a[]="Johanna"; $a[]="Kitty"; $a[]="Linda"; $a[]="Nina"; $a[]="Ophelia"; $a[]="Petunia"; $a[]="Amanda"; $a[]="Raquel"; $a[]="Cindy"; $a[]="Doris"; $a[]="Eve"; $a[]="Evita"; $a[]="Sunniva"; $a[]="Tove"; $a[]="Unni"; $a[]="Violet"; $a[]="Liza"; $a[]="Elizabeth"; $a[]="Ellen"; $a[]="Wenche"; $a[]="Vicky"; //get the q parameter from URL $q=$_GET["q"]; //lookup all hints from array if length of q>0 if (strlen($q) > 0){ $hint=""; for($i=0; $i<count($a); $i++){ if (strtolower($q)==strtolower($a[$i])){ $hint=$a[$i]; } } } // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint == ""){ $response="no suggestion"; } else{ $response=$hint; } //output the response echo $response; ?>
|
Paranoid (IV) Inmate From: Paris, France |
posted 09-17-2009 13:10
Yes, you could have your php page return an object and evaluate it into a variable, but actually you don't need to use complex objects if you're only hinting at text values. |
Bipolar (III) Inmate From: |
posted 09-17-2009 15:16
Thank you very much for the advice i didn't think of it that way. but im actually not trying to get a hint. I started with a hint but now i want to use it like a username availability checker. So if there is an exact match they will not be able to use it. I just didnt change the code to much cause i was being lazy.. |
Paranoid (IV) Inmate From: Paris, France |
posted 09-17-2009 16:43
Ahh, sorry I misunderstood you. code: $response = "{property1: '1', property2: '2'}"; [...] var myObject = eval(xmlhttp.responseText); alert(myObject.property1);
|
Obsessive-Compulsive (I) Inmate From: |
posted 05-03-2010 13:23
edit tp: spam removed. |