Thursday 6 June 2013

Dynamic Autocomplete using php

index.php:<
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Auto Complete Input box</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(){
 $("#tag").autocomplete("autocomplete.php", {
  selectFirst: true
 });
});
</script>
</head>
<body>
<label>Tag:</label>
<form action="" method="post">
<input name="tag" type="text" id="tag" size="20"/> 
 <input type="submit" value="submit" name="gt"/>
<form>
</body>
<?php
if(isset($_POST["gt"]))
{
$mysql=mysql_connect("localhost","root","123");
mysql_select_db("hi",$mysql);
mysql_query("insert into tag (name) values ('".$_POST["tag"]."')");
echo $_POST["tag"];
}
?>
</html>
autocomplete.php:
<?php
 $q=$_GET['q'];
 $my_data=mysql_real_escape_string($q);
 $mysqli=mysqli_connect('localhost','root','123','hi') or die("Database Error");
 $sql="SELECT name FROM tag WHERE name LIKE '%$my_data%' ORDER BY name";
 $result = mysqli_query($mysqli,$sql) or die(mysqli_error());
 
 if($result)
 {
  while($row=mysqli_fetch_array($result))
  {
   echo $row['name']."\n";
  }
 }
?>


Database :
id--->int(4)--->Autocomplete
Name--->varchar(50)
Download

No comments:

Post a Comment