looking for a good autocomplete script

Discussion in 'Programming' started by neil patrick harris, Feb 9, 2008.

  1. #1
    neil patrick harris, Feb 9, 2008 IP
  2. Im The ONE

    Im The ONE Peon

    Messages:
    800
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #2
    xyz.htm
    <html>
    <head>
    <script src="suggest.js"></script> 
    </head>
    <body>
    <form> 
    First Name:
    <input type="text"
    onkeyup="suggest(this.value)">
    </form>
    <div id="suggestions"></div>
    </body>
    </html> 
    HTML:
    suggest.js
    var xmlHttp
    
    function suggest(string)
    {
    if (string.length==0)
      { 
      document.getElementById("suggestions").innerHTML=" ";
      return;
      }
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
      alert ("NO ajax support");
      return;
      } 
    var url="suggest.php";
    url=url+"?word="+string;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    document.getElementById("suggestions").innerHTML='generating..';
    } 
    
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4)
    { 
    document.getElementById("suggestions").innerHTML=xmlHttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
      {
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
    }
    Code (markup):
    suggest.php
    
    $sql = mysql_query("select * from abc where words like '".$_get['word']."');
    while($q=mysql_fetch_array($sql)) {
    echo $q['word'];
    echo ("<br />");
    }
    
    PHP:
    Just an attempt dunno how google displays them in input field itself
     
    Im The ONE, Feb 9, 2008 IP
  3. neil patrick harris

    neil patrick harris Peon

    Messages:
    288
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks but it says theres an error on line 5. Do I need an mysql database for this?
     
    neil patrick harris, Feb 9, 2008 IP
  4. daringtakers

    daringtakers Well-Known Member

    Messages:
    808
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #4
    daringtakers, Feb 11, 2008 IP
  5. neil patrick harris

    neil patrick harris Peon

    Messages:
    288
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks I'll check it out. I just want to make suggestions for the most common searches in my search form. Like if someone types prison break, the latest episodes will come up. "Prison Break S03E12" Which gets the best search results.
     
    neil patrick harris, Feb 13, 2008 IP