0

I am trying to create a cascading dropwdown for state, city and zip. I have part of it working like when a user chooses a state the corresponding city list comes up, but when a user chooses a city no zips come up. Not sure what i am missing or doing wrong. Can someone please help me with this?

index.php

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body>
<div id="container">
  <div id="body">
    <div id="dropdowns">
       <div id="center" class="cascade">
          <?php
        $sql = "SELECT DISTINCT state FROM tbl_zip ORDER BY state ASC";
        $query = mysqli_query($con, $sql);
        ?>
            <label>State:
            <select name="state" id = "state">
              <option value="">Please Select</option>
              <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
              <option value="<?php echo $rs["state"]; ?>"><?php echo $rs["state"]; ?></option>
              <?php } ?>
            </select>
            </label>
          </div>

        <div class="cascade" id="city"></div> 

          <div id="zip" class="cascade"></div> 
        </div>
    </div>
  </div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("select#state").change(function(){

    var state =  $("select#state option:selected").attr('value'); 
    //alert(state); 
    $("#city").html( "" );
    $("#zip").html( "" );
    if (state.length > 0 ) { 
        //alert(state.length);
     $.ajax({
            type: "POST",
            url: "fetch_state.php",
            data: "state="+state,
            cache: false,
            beforeSend: function () { 
                $('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#city").html( html );
            }
        });
    } 
});

$("select#city").change(function(){

    var city = $("select#city option:selected").attr('value');
   // alert(state_id);
    if (city.length > 0 ) { 
     $.ajax({
            type: "POST",
            url: "fetch_city.php",
            data: "city="+city,
            cache: false,
            beforeSend: function () { 
                $('#zip').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#zip").html( html );
            }
        });
    } else {
        $("#zip").html( "" );
    }
});


});
</script>
</body>
</html>

fetch_state.php

<?php

include("connection.php");
//var_dump($_POST);
$state = trim(mysqli_escape_string($con, $_POST["state"]));

$sql = "SELECT DISTINCT city FROM tbl_zip WHERE state = '".$state ."' ORDER BY city";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>City: 
<select name="city" id="city">
    <option value="">Please Select</option>
    <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
    <option value="<?php echo $rs["city"]; ?>"><?php echo $rs["city"]; ?></option>
    <?php } ?>
</select>
</label>
<?php 
    }

?>

fetch_city.php

<?php

include("connection.php");
$city = trim(mysqli_escape_string($con, $_POST["city"]));

$sql = "SELECT DISTINCT zip FROM tbl_zip WHERE city = '".$city ."' ORDER BY zip ASC";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>zip: 
<select name="zip" id="zip">
    <option value="">Please Select</option>
    <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
    <option value="<?php echo $rs["zip"]; ?>"><?php echo $rs["zip"]; ?></option>
    <?php } ?>
</select>
</label>
<?php 
    }

?>
Brad Hazelnut
  • 1,603
  • 5
  • 21
  • 33
  • It looks like select#city does not exist when you assign the event handler, and you overwrite the entire city element every time someone picks a state. You need to reassign the handler or use a promise, or don't just replace all the html every time something changes. – James Aug 04 '14 at 13:59
  • Change `$("select#city").change(function(){` to `$(document).on('change', "select#city",(function(){` as you are trying to bind to a dynamically added element to the DOM. Since it is not there when the DOM is ready, it is not binding. – Sean Aug 04 '14 at 13:59
  • possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Sean Aug 04 '14 at 14:03

2 Answers2

1

Here is the link, I think this will help :

http://www.91weblessons.com/php-ajax-country-state-city-drop-down/

happyCoding :D

GoalDone
  • 345
  • 1
  • 3
  • 15
0

Move the javascript code

$("select#city").change(function(){

var city = $("select#city option:selected").attr('value');
// alert(state_id);
if (city.length > 0 ) { 
 $.ajax({
        type: "POST",
        url: "fetch_city.php",
        data: "city="+city,
        cache: false,
        beforeSend: function () { 
            $('#zip').html('<img src="loader.gif" alt="" width="24" height="24">');
        },
        success: function(html) {    
            $("#zip").html( html );
        }
    });
} else {
    $("#zip").html( "" );
}
});

in fetch_state.php file along the generation of select.

MTM
  • 919
  • 10
  • 22