infolink

Monday 20 April 2015

Auto Complete Textbox in PHP

In this tutorial you will learn how a auto complete textbox using php. It so simple and easy.
here i will use the java script function for this task.

Auto Complete textbox in php
Auto Complete textbox in php




first of all you will create the two php files.

  1. index.php
  2. autocomplete.php

Step 1:

when you create the index.php file then open it and past the following code in it.

Code:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
 <title>Auto Complete textbox in php | www.developerqueries.blogspot.com</title>  
 <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />  
 <script type="text/javascript" src="jquery.js"></script>  
 <script type="text/javascript" src="jquery.autocomplete.js"></script>  
 <script>  
 $(document).ready(function(){  
  $("#student").autocomplete("autocomplete.php", {  
           selectFirst: true  
      });  
 });  
 </script>  
 </head>  
 <body>  
   <label>Student:</label>  
   <input name="student" type="text" id="student" size="20" />  
 </body>  
 </html>  

Step 2:

Now open the autocomplete.php file which is already created, and past the following code in it.

Code:

 <?php  
      $q=$_GET['q'];  
      $my_data=mysql_real_escape_string($q);  
      $mysql=mysql_connect('localhost','root','') or die("Database Error");  
      $db=mysql_select_db('test') or die("Database Error");  
      $sql="SELECT std_name FROM student_info WHERE std_name LIKE '%$my_data%' ORDER BY std_name";  
      $result = mysql_query($sql,$mysql);  
      if($result)  
      {  
           while($row=mysql_fetch_array($result))  
           {  
                echo $row['std_name']."\n";  
           }  
      }  
 ?>  

Now your auto complete textbox is ready for working. run your index.php file and use this functionality.

No comments:

Post a Comment