Johnson_7 0 Newbie Poster

Hi

Dynamic Dependent Select Box last one not working Bank,State,District is working branch ont working please check below code

Please help.

index.php

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
.select-boxes{width: 280px;text-align: center;}
select {
    background-color: #F5F5F5;
    border: 1px double #FB4314;
    color: #55BB91;
    font-family: Georgia;
    font-weight: bold;
    font-size: 14px;
    height: 39px;
    padding: 7px 8px;
    width: 250px;
    outline: none;
    margin: 10px 0 10px 0;
}
select option{
    font-family: Georgia;
    font-size: 14px;
}
</style>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#country').on('change',function(){
        var countryID = $(this).val();
        if(countryID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'country_id='+countryID,
                success:function(html){
                    $('#state').html(html);
                    $('#city').html('<option value="">Select District</option>'); 
                }
            }); 
        }else{
            $('#state').html('<option value="">Select Bank</option>');
            $('#city').html('<option value="">Select State</option>'); 
        }
    });

     $('#state').on('change',function(){
        var stateID = $(this).val();
        if(stateID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
               data:'state_id='+stateID,
                success:function(html){
                    $('#city').html(html);
                    $('#branch').html('<option value="">Select Branch</option>'); 
                }
            }); 
        }else{
            $('#city').html('<option value="">Select State</option>');
            $('#branch').html('<option value="">Select Branch</option>'); 
        }
    });

    $('#city').on('change',function(){
        var cityID = $(this).val();
        if(cityID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'city_id='+cityD,
                success:function(html){
                    $('#branch').html(html);
                }
            }); 
        }else{
            $('#branch').html('<option value="">Select Branch</option>'); 
        }
    });
});
</script>
</head>
<body>
    <div class="select-boxes">
    <?php
    //Include database configuration file
    include('dbConfig.php');

    //Get all country data
    $query = $db->query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;
    ?>
    <select name="country" id="country">
        <option value="">Select Bank</option>
        <?php
        if($rowCount > 0){
            while($row = $query->fetch_assoc()){ 
                echo '<option value="'.$row['country_id'].'">'.$row['country_name'].'</option>';
            }
        }else{
            echo '<option value="">Country not available</option>';
        }
        ?>
    </select>

    <select name="state" id="state">
        <option value="">Select State</option>
    </select>

    <select name="city" id="city">
        <option value="">Select District</option>
    </select>

    <select name="branch" id="branch">
        <option value="">Select Branch</option>
    </select>

    </div>
</body>
</html>

ajaxdata.php

<?php
//Include database configuration file
include('dbConfig.php');

if(isset($_POST["country_id"]) && !empty($_POST["country_id"])){
    //Get all state data
    $query = $db->query("SELECT * FROM states WHERE country_id = ".$_POST['country_id']." AND status = 1 ORDER BY state_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display states list
    if($rowCount > 0){
        echo '<option value="">Select State</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
        }
    }else{
        echo '<option value="">State not available</option>';
    }
}

if(isset($_POST["state_id"]) && !empty($_POST["state_id"])){
    //Get all state data
    $query = $db->query("SELECT * FROM cities WHERE state_id = ".$_POST['state_id']." AND status = 1 ORDER BY city_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display states list
    if($rowCount > 0){
        echo '<option value="">Select District</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
        }
    }else{
        echo '<option value="">City not available</option>';
    }
}

if(isset($_POST["city_id"]) && !empty($_POST["city_id"])){
    //Get all city data
    $query = $db->query("SELECT * FROM branchs WHERE city_id = ".$_POST['city_id']." AND status = 1 ORDER BY branch_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display cities list
    if($rowCount > 0){
        echo '<option value="">Select Brach</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['branch_id'].'">'.$row['branch_name'].'</option>';
        }
    }else{
        echo '<option value="">Branch not available</option>';
    }
}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.