suggest in ajax..

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2009
Posts: 5
Reputation: jyotidagia is an unknown quantity at this point 
Solved Threads: 0
jyotidagia jyotidagia is offline Offline
Newbie Poster

suggest in ajax..

 
0
  #1
Nov 2nd, 2009
i hav this code for autosuggest..
it retrieves the desired values from the datbase..
and produces a list..like in google..

however the values in the list cannot be selected using the arrow keys..
could anybody familiar with google suggest help me out with this..

am using this for an textbox in html file..!!

thanks in advance..
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. /*livesearch.js*/
  2. var xmlhttp;
  3.  
  4. function showResult(str)
  5. {
  6. if (str.length==0)
  7. {
  8. document.getElementById("livesearch").innerHTML="";
  9. document.getElementById("livesearch").style.border="0px";
  10. return;
  11. }
  12. xmlhttp=GetXmlHttpObject()
  13. if (xmlhttp==null)
  14. {
  15. alert ("Your browser does not support XML HTTP Request");
  16. return;
  17. }
  18. var url="livesearch.php";
  19. url=url+"?q="+str;
  20. url=url+"&sid="+Math.random();
  21. xmlhttp.onreadystatechange=stateChanged ;
  22. xmlhttp.open("GET",url,true);
  23. xmlhttp.send(null);
  24. }
  25. function stateChanged()
  26. {
  27. if (xmlhttp.readyState==4)
  28. {
  29. document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
  30. document.getElementById("livesearch").style.border="1px solid #A5ACB2";
  31. // document.getElementById("livesearch").style.cursor="default";
  32. }
  33. }
  34.  
  35. function GetXmlHttpObject()
  36. {
  37. if (window.XMLHttpRequest)
  38. {
  39. // code for IE7+, Firefox, Chrome, Opera, Safari
  40. return new XMLHttpRequest();
  41. }
  42. if (window.ActiveXObject)
  43. {
  44. // code for IE6, IE5
  45. return new ActiveXObject("Microsoft.XMLHTTP");
  46. }
  47. return null;
  48. }

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. /*livesearch.php*/
  2. <?php
  3.  
  4. $strSearchVal = $_GET['q'];
  5.  
  6. if (strlen($strSearchVal) < 1) {
  7. return false;
  8. }
  9.  
  10. // Opens a connection to a MySQL server
  11. $connection=mysql_connect ("localhost", "root", "");
  12. if (!$connection) {
  13. die('Not connected : ' . mysql_error());
  14. }
  15. // Sanitize User Input for Security
  16. $strSearchVal = mysql_real_escape_string($strSearchVal);
  17.  
  18.  
  19. // Set the active MySQL database
  20. $db_selected = mysql_select_db("hospitals_info", $connection);
  21. if (!$db_selected) {
  22. die ('Can\'t use db : ' . mysql_error());
  23. }
  24.  
  25. $strSQL = "SELECT DISTINCT area FROM hospital WHERE area like '$strSearchVal%'";
  26. $result = mysql_query($strSQL)
  27. or
  28. die("Query to get blah failed with error: ".mysql_error());
  29.  
  30.  
  31. while ($a=mysql_fetch_array($result)) {
  32. echo $a['area']."<br />";
  33. }
  34.  
  35.  
  36.  
  37. ?>
Last edited by Ezzaral; Nov 2nd, 2009 at 1:18 pm. Reason: Fixed code tag.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 121
Reputation: sam023 is an unknown quantity at this point 
Solved Threads: 2
sam023 sam023 is offline Offline
Junior Poster
 
0
  #2
Nov 2nd, 2009
auto suggest and auto autocomplete are two different things..!! try to google for autocomplete script
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 373 | Replies: 1
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC