Ryflex Posted June 1, 2011 Share Posted June 1, 2011 Hi all, I'm experimenting with Ajax and I'm kinda stuck. Underneath is my code please help me out. <html> <head> <script type="text/javascript"> function showHint(str) { var xmlhttp; 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","ajax_test.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <h3>Start typing a name in the input field below:</h3> <form action=""> First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" /> </form> <p>Suggestions: <span id="txtHint"></span></p> </body> </html> <?php // Fill up array with names //Include database connection details require_once('config.php'); //get the q parameter from URL $q=$_GET["q"]; //Connectie naar mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Selecteer database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $query = "SELECT * FROM users WHERE voornaam ='".$q."'"; $result = mysql_query($query) or die('Query1 failed: ' . mysql_error()); $totalRows = mysql_num_rows($result); $a = array(); for ($j=0; $j<$totalRows; $j++) { $a[$j] = mysql_fetch_row($result); } //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(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 values if ($hint == "") { $response="no suggestion"; } else { $response=$hint; } //output the response echo $response; ?> Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/ Share on other sites More sharing options...
requinix Posted June 1, 2011 Share Posted June 1, 2011 Stuck on what? Help you out with what? Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1223657 Share on other sites More sharing options...
Ryflex Posted June 1, 2011 Author Share Posted June 1, 2011 Hi, Sorry actually forgot ta ask the question. When I type something in the textfield it should give me some suggestions from my db. Somehow it keeps telling me there are no suggestions. I think I'm doing something wrong in the call to the db but I tried different approaches and it still won't work. Anyone have a clue? Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1223661 Share on other sites More sharing options...
requinix Posted June 1, 2011 Share Posted June 1, 2011 $a[$j] will be an array, not a string. Reconsider what your code does. Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1223756 Share on other sites More sharing options...
Ryflex Posted June 1, 2011 Author Share Posted June 1, 2011 We'll it kind of should be an array so it can itterate through the array to give the suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1223763 Share on other sites More sharing options...
requinix Posted June 1, 2011 Share Posted June 1, 2011 I meant more "look at your code and see what it tries to do with $a". For instance, this: substr($a[$i],0,strlen($q)) You can't substr on an array. Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1223792 Share on other sites More sharing options...
Ryflex Posted June 2, 2011 Author Share Posted June 2, 2011 Hi, This is the site which i'm using the base code from and modifying it to my use. http://w3schools.com/ajax/ajax_aspphp.asp There they manually fill the array and do the same. So I still can't make it work. Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1223938 Share on other sites More sharing options...
Ryflex Posted June 2, 2011 Author Share Posted June 2, 2011 Can anyone please help me out here Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224118 Share on other sites More sharing options...
Ryflex Posted June 2, 2011 Author Share Posted June 2, 2011 Maybe it helps to know that when I type in one of the names in de database which should be "predicted" it gives the following error: Warning: mysql_fetch_row() expects parameter 1 to be resource, string given in /customers/ryflex.nl/ryflex.nl/httpd.www/ajax_test.php on line 31 no suggestion When I put in 1 letter more or less it jumps back to no suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224318 Share on other sites More sharing options...
requinix Posted June 2, 2011 Share Posted June 2, 2011 Their $a is an array of strings. Your $a is an array of arrays. That's the biggest difference: mysql_fetch_row() returns an array of fields, not just one particular field. Instead try $a[] = mysql_fetch_object($result)->predicted; As for mysql_query() returning a string, I've never heard of that happening. What is your code? Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224341 Share on other sites More sharing options...
Ryflex Posted June 3, 2011 Author Share Posted June 3, 2011 ok after modyfing the php file to what you told me th query i run is now th following and I still don't get a result. the error when I fill out an entire name is gone as well. $query = "SELECT * FROM users WHERE voornaam ='".$q."'"; $result = mysql_query($query) or die('Query1 failed: ' . mysql_error()); $totalRows = mysql_num_rows($result); $a[] = mysql_fetch_object($result)->predicted; Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224488 Share on other sites More sharing options...
requinix Posted June 3, 2011 Share Posted June 3, 2011 ... Is that your actual code? If not, post it. Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224898 Share on other sites More sharing options...
Ryflex Posted June 3, 2011 Author Share Posted June 3, 2011 nvm solved in php part of the forum Quote Link to comment https://forums.phpfreaks.com/topic/238132-making-a-livesearch/#findComment-1224903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.