Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing a project where I want dropdown selection based autocomplete values in textbox.


My index.php coding:

XML
<!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></title>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script>
$(document).ready(function(){
 $("#tag").autocomplete("autocomplete.php", {
        selectFirst: true
    });
});
</script>
<script>
   $(document).ready(function() {
      $("#city").change(function(){
          var city = $("#city").val();
          $("#stage").load('autocomplete.php', {"city":city} );
      });
   });
</script>
</head>
<body>
<div>Select City</div>
<select id="city" name="city">
<option value="select">Select</option>
<option value="Chennai">Chennai</option>
<option value="Madurai">Madurai</option>
<option value="Salem">Salem</option>
<option value="Trichy">Trichy</option>
</select><br />
<input type="text" id="tag" class="tag" size="20" /><br />
<div id="stage">
</div>
</form>
</body>
</html>



In index page on selecting city the value is passed to autocomplete.php using jquery.

my autocomplete.php coding:

PHP
<?php
if($_REQUEST["city"])
{
   $city = $_REQUEST['city'];
}
?>
<?php
echo $city;
$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
$mysqli=mysql_connect('localhost', 'root', '');
if (!$mysqli)
{
   die('Could not connect: ' . mysql_error());
}
if(!mysql_select_db("details"))
{
  die('Could not connect database: ' . mysql_error());
}
    $sql="SELECT localty FROM localty where localty like '%$my_data%' and city='$city'";
    $result = mysql_query($sql);

    if($result)
    {
        while($row=mysql_fetch_array($result))
        {
            echo $row['localty']."\n";
        }
    }
?>


on changing the city value in autocomplete.php the value is displayed but entering firstletter in textbox for autocomplete the city is not displaying.

Any Suggestions Please.
Posted
Updated 30-Jan-15 19:30pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900