Hobby99
Hobby99

Reputation: 703

Populating multiple dependent drop downs from database

I have 2 drop-down menues. The first I populated with possible continents and want the second drop-down to what include all countries depending on the continent that was selected in the first menu. I have only 1 mysql table with columns: continent --> country. Currently there are all countries from all continents in the drop-down even when a continent was chosen. Thank you for your help! Here is my code:

HTML

<input type="text" class="autosuggest" id="autosuggest2" placeholder="Select Country...">
<div class="country">
<ul class="result" id="result2"></ul>
</div>
<input type="text" class="autosuggest" id="autosuggest3" placeholder="Select Area...">
<div class="area">
<ul class="result" id="result3"></ul>
</div>

jQuery / Ajax

$(document).ready(function() { 
$('#autosuggest1').keyup(function() {
    var continent = $(this).attr('value');
    $.post('php/dropdown.php', {continent:continent}, function(data) {
        $('#result1').html(data);

        $('.result li').click(function() {
            var result_value = $(this).text();
            $('#autosuggest1').attr('value', result_value);
            $('#result1').html('');
            $('#result1').focusout('');
        });
    });
});

$('#autosuggest2').keyup(function() {
    var country = $(this).attr('value');
    $.post('php/dropdown.php', {country:country}, function(data) {
        $('#result2').html(data);

        $('.result li').click(function() {
            var result_value = $(this).text();
            $('#autosuggest2').attr('value', result_value);
            $('#result2').html('');
            $('#result2').focusout('');
        });
    });
});
});

PHP

require_once '../connect/connectdropdown.php';

if (isset($_POST['continent']) == true && empty($_POST['continent']) == false) {
$continent = mysql_real_escape_string($_POST['continent']);
$query = mysql_query("SELECT DISTINCT `continent` FROM `area` WHERE `continent`    LIKE '$continent%'");
while (($row = mysql_fetch_assoc($query)) !== false) {
    echo '<li>', $row['continent'], '</li>';
}   
}

if (isset($_POST['country']) == true && empty($_POST['country']) == false) {
$country = mysql_real_escape_string($_POST['country']);
$query = mysql_query("SELECT DISTINCT `country` FROM `area` WHERE `country` LIKE '$country%'");
while (($row = mysql_fetch_assoc($query)) !== false) {
    echo '<li>', $row['country'], '</li>';
}   
}

Upvotes: 1

Views: 1500

Answers (1)

KoU_warch
KoU_warch

Reputation: 2150

i wonder if you are still on this. Anyway a couple of things i would suggest:

Do only one query, pass it to json, and give it to javascript.

PHP

require_once '../connect/connectdropdown.php';
$id=0;
$result=array();
$query = mysql_query("SELECT * FROM `area` ");
while (($row = mysql_fetch_assoc($query)) !== false) {
   $result[$id]=$row;
   $id++;
}   
echo json_encode($result);

Javascript

$(document).ready(function() { 
      var $mysqlData;
      $.post('php/dropdown.php', {continent:continent}, function(data) {

        //Json Data from server:
        alert('JSON data: '+data)
        //you can transform the data from PHP-Json to Javascript Object
        $mysqlData = jQuery.parseJSON(data)
        //now you can access your data like:
        alert ('My first continent: '+$mysqlData[0].continent+' and it\'s first country: '+$mysqlData[0].country)
      });

      $('#autosuggest1').keyup(function() {
          //Now you can use $mysqlData inside your logic
      })

      $('#autosuggest2').keyup(function() {
          //You can use $mysqlData here too
      })

});

i did not test this code, there might be some errors but that the logic i suggest you to take.

Upvotes: 1

Related Questions