sepoto Posted February 25, 2012 Share Posted February 25, 2012 I have been studying the example located here: http://www.w3schools...hp_ajax_php.asp In the example the '$response' returned from the PHP script is a single string variable. What if I wanted to return an array of data? How would the array of data then be handled on the JavaScript side? I am posting code below: <?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>0if (strlen($q) > 0) { $hint=""; for($i=0; $i<count($a); $i++) { if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { $hint=$a[$i]; } else { $hint=$hint." , ".$a[$i]; } } } }// Set output to "no suggestion" if no hint were found// or to the correct valuesif ($hint == "") { $response="no suggestion"; }else { $response=$hint; }//output the responseecho $response;?> <html><head><script type="text/javascript">function showHint(str){if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; }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; } }xmlhttp.open("GET","gethint.php?q="+str,true);xmlhttp.send();}</script></head><body> <p><b>Start typing a name in the input field below:</b></p><form> First name: <input type="text" onkeyup="showHint(this.value)" size="20" /></form><p>Suggestions: <span id="txtHint"></span></p> </body></html> Thank you all very much! Link to comment Share on other sites More sharing options...
chasethemetal Posted February 25, 2012 Share Posted February 25, 2012 $output = array( 'response' => $a); echo json_encode( $output ); Now that is turned into a javascript object you can access the information in the javascript in the success handler, like so data.response. You will have all of the information thats contained in $a. You can use something like $.each( ) to iterate through the different options, or you could access them like var anna = data.response[0]... Try looking up .ajax in jQuery. Much easier to work with then hard coding ajax calls in javascript. Hope this points ya in the right direction, but the key is changing the array to a JSON object. Link to comment Share on other sites More sharing options...
birbal Posted February 25, 2012 Share Posted February 25, 2012 If you need to take out PHP array to JS you can use [http://php.net/json_encode]json_encode()[/url] to encode it in json format after that JS can parse that reponse and can evaluate JS array Link to comment Share on other sites More sharing options...
sepoto Posted February 26, 2012 Author Share Posted February 26, 2012 It is very simple and a very powerfully tool. I like it. Thanks again. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.