Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
I am trying to do autocomplete function based on dropdown selection value.

My index.php page:

     <div>Select City</div>
     <select id="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="search" />
     <script>
      $(document).ready(function(){
      $('#search').autocomplete({
      minLength: 1,
      source: function(query, process) {
        var res = $('#city').val();
        $.ajax({
            url: 'autocomplete.php',
            type: 'GET',
            data: "src="+res + "&value=" + $('#search').val(),
            dataType: 'JSON',
            async: true,
            success: function(data) {
                process(data);
            }
        });
    }
});
});
</script>

My autocomplete.php page:

 <?php
 if (isset($_GET['src'])) {
 $city = $_GET['src'];
 echo $city;
 }
 ?>
 <?php
 $loc = $_GET['value'];
 echo $loc;
 $conn=mysql_connect('localhost', 'root', '');
 if (!$conn)
 {
 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 '%".$loc."%' and city='$city'";
 echo $sql;
 $result = mysql_query($sql);

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

Based on the city selection dropdown value,in search textbox localty should be displayed on entering first letter.But I'm not getting values in autocomplete.php page.

Please give any suggestions.
Posted
Comments
Pranay Rana 2-Feb-15 2:20am    
please check updated answer go through the link of the answer

1 solution

the way you are passing data is wrong that may be one reason of not working. as you want to pass data as get request you should append the data in the query string like this


JavaScript
url: 'autocomplete.php?src='+res + '&value=' + $("#search").val(),



or if you want to pass data as json than do proper formatting of passed data as below , i.e. pass data in json formate as show below

C#
data: {
                           src: res,
                           value: $('#search').val();
                        }


Also refer this example : http://www.smarttutorials.net/jquery-autocomplete-search-using-php-mysql-and-ajax/[^]

i hope by doing this your code will work
 
Share this answer
 
v2
Comments
Member 11310933 2-Feb-15 2:16am    
@Pranay Rana Sorry sir it is not working.
Pranay Rana 2-Feb-15 2:21am    
Please check updated answer and also go though link of answer

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