user2178637
user2178637

Reputation: 117

display 3rd drop down list based on 1st 2 dropdown ids

I want to display an interrelated drop down list. When 1st dropdown ie Area list selected, related Sectors list will be populated in 2nd dropdown. Till this i am able to do it. But After 2nd dropdown ie Sector is selected i want to display the plots which will be based on both 'Area' and 'Sector'. This part i am not able to do it. I am able to display the plots based on 'Sector' only not the both Area and Sector.

Here is my code

Fetching Sectors

function showItems(sel) {
    var cat_id = sel.options[sel.selectedIndex].value;  
    $("#output1").html( "" );
    $("#output2").html( "" );
    if (cat_id.length > 0 ) {

     $.ajax({
            type: "POST",
            url: "fetch_sectors.php",
            data: "cat_id="+cat_id,
            cache: false,
            beforeSend: function () {
                $('#output1').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#output1").html( html );
            }
        });
    }
}

Fetching Plots (based only Sector only )

function showItemDet(sel) {
    var item_id = sel.options[sel.selectedIndex].value; 
    $("#output2").html( "" ); 
    if (item_id.length > 0 ) { 
     $.ajax({
            type: "POST",
            url: "fetch_plot.php",
            data: "item_id="+item_id,
            cache: false,
            beforeSend: function () { 
                $('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#output2").html( html );
            }
        });
    }
}

How can i do it?.

***EDIT****

i did something like this

<script>
function showPlots(area, sector) {
    var item_id = sel.options[sel.selectedIndex].value; 
    var cat_id = sel.options[sel.selectedIndex].value; 
    $("#output2").html( "" ); 
    if (item_id.length > 0 ) { 
     $.ajax({
            type: "POST",
            url: "fetch_plot.php",
            data: {area: item_id, sector:cat_id},
            cache: false,
            beforeSend: function () { 
                $('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#output2").html( html );
            }
        });
    }
}
</script>

and in fetch_plots

   $area = ($_REQUEST["area"] <> "") ? trim( addslashes($_REQUEST["area"])) : "";
echo $area;
$sector = ($_REQUEST["sector"] <> "") ? trim( addslashes($_REQUEST["sector"])) : "";
if ($item_id <> "" && $cat_id<>"") { 
$sql = "SELECT * FROM plots where area_id=".$area." AND sec_id=".$sector."";
echo $sql;

$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>

<select name="plot">
    <option value="">Select Plot</option>
    <?php while ($rs = mysqli_fetch_array($query)) { ?>
    <option value="<?php echo $rs["plot_id"]; ?>"><?php echo $rs["name"]; ?></option>
    <?php } ?>
</select>

<?php 
    }
}
?>

Upvotes: 0

Views: 74

Answers (1)

DevlshOne
DevlshOne

Reputation: 8457

     $.ajax({
            type: "POST",
            url: "fetch_plots.php",
            data: {area:area, sector:sector},
            cache: false,
            beforeSend: function () { 
                $('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#output2").html( html );
            }
        });

fetch_plots.php

$area = (isset($_REQUEST["area"]) ? intval($_REQUEST["area"]) : 0);
// assuming "$area" is an integer, not a varchar
echo $area;
$sector = (isset($_REQUEST["sector"]) ? intval($_REQUEST["sector"]) : 0);
// assuming "$sector" is an integer, not a varchar
echo $sector;
if ( !empty($area) && !empty($sector) ) { 
    $sql = "SELECT * FROM plots where area_id=" . $area . " AND sec_id=" . $sector;
echo $sql;
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
    $query = mysqli_query($con, $sql);
?>
    <select name="plot">
        <option value="">Select Plot</option>
        <?php 
            while ($rs = mysqli_fetch_array($query)) {
        ?>
                <option value="<?php echo $rs["plot_id"]; ?>"><?php echo $rs["name"]; ?></option>
         <?php } ?>
     </select>
<?php 
    }
}
?>

Upvotes: 1

Related Questions