jquery autocomplete using php and mysql

autocomplete.php

<html>
   <head>
  <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
        <link rel="stylesheet" type="text/css"
        href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />

  <script type="text/javascript">
                $(document).ready(function(){
                 $("#name").autocomplete({
                  source:'getautocomplete.php',
                  minLength:1
                 });
                });
  </script>
   </head>

   <body>

      <form method="post" action="">
             Name : <input type="text" id="name" name="name" />
      </form>

   </body>
<html>


getautocomplete.php

<?php
 mysql_connect("localhost","root","");
 mysql_select_db("php_autocomplete");
 $term=$_GET["term"];
 $query=mysql_query("SELECT * FROM student where name like '%".$term."%' order by name ");
 $json=array();
    while($student=mysql_fetch_array($query)){
         $json[]=array(
                    'value'=> $student["name"],
                    'label'=>$student["name"]." - ".$student["id"]
                        );
    }
 echo json_encode($json);
?>



karizma

Blogger Since 2012 Love to write personal experience and share with others.

Post a Comment

Previous Post Next Post