Ajax search box with Php+mysql

Discussion in 'PHP' started by dj_ron, May 16, 2010.

  1. #1
    Hi All,

    Need some quick help. Please see my code below. When I start typing in the search box the Suggestions are shown below as a table but i need them like Google suggest(live search).

    Please help me on this.
    
    HTML FILE
    
    <html>
    <head>
    <script type="text/javascript">
    function showUser(str)
    {
    if (str=="")
      {
      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","getuser.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    
    <form>
    <input type="text" id="txt1" size="30" onkeyup="showUser(this.value)" autocomplete="off" />
    <div id="livesearch"></div>
    </form>
    <br />
    <div id="txtHint"><b>Your group will be listed here</b></div>
    
    </body>
    </html> 
    Code (markup):
    PHP FILE
    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("needsesearch", $con);
    
    $sql="SELECT title FROM `jos_sections` WHERE title like  '%".$q."%' ";
    
    $result = mysql_query($sql);
    
    echo "<table border='1'>
    <tr>
    
    
    
    
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
    
      echo "<td>" . $row['title'] . "</td>";
    
     
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    ?> 
    Code (markup):
     
    dj_ron, May 16, 2010 IP
  2. vetrivel

    vetrivel Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,
    If you get the result correctly and you only have design issue then JUST remove the border =1 " and ADD "cellpadding=0 cellspacing=0" .
    May it will help you.



     
    vetrivel, May 16, 2010 IP
  3. dj_ron

    dj_ron Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi vetrivel, its not just a design issue. I need the results to get displayed just below the search box and when we move the down arrow key it should get selected.
     
    dj_ron, May 16, 2010 IP
  4. dj_ron

    dj_ron Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    help help help help
     
    dj_ron, May 17, 2010 IP
  5. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Can i see the working demo?
     
    roopajyothi, May 17, 2010 IP
  6. dj_ron

    dj_ron Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hi roopajyothi,

    please see the screenshots below. For example when i type 'a', it finds all the results with 'a'. BUt i need it with the suggest box.

    [​IMG]

    The result
    [​IMG]

    Need it like this.

    [​IMG]


     
    dj_ron, May 18, 2010 IP
  7. abstractworld

    abstractworld Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can use some js library. like yui dojo mootools jquery etc...doing it yourself will make lots of issues. Generally browser compatibility issues...
    so better use some js lib. I used yui for this... but others are good too.

    Thanks...
     
    abstractworld, May 19, 2010 IP