Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

php - Dynamic dropdown with auto select one option and it's sub-dropdown options

I have a dropdown in which one is for states and second one is it's sub category, which shows name of cities on basis of first dropdown. Right now user need to select one category then subcategories load, What change I want is "To set one value as default and load it's subcategories too" at load of page, and further user select different category so it should load it's subcategories accordingly.

Index.php:

<form name="insert" action="" method="post">
    <table width="100%" height="117" border="0">
        <tr>
            <th width="27%" height="63" scope="row">Sate :</th>
                <td width="73%">
                    <select onChange="getdistrict(this.value);" name="state" id="state" class="form-control">
                    <option value="">Select</option>
                    <?php $query =
                        mysqli_query($con,"SELECT * FROM state");
                        while($row=mysqli_fetch_array($query))
                        { ?>
                            <option value="<?php echo $row['StCode'];?>">
                                <?php echo $row['StateName'];?>
                            </option>
                    <?php }?>
                    </select>
                </td>
            </tr><tr>
            <th scope="row">District :</th>
            <td>
                <select name="district" id="district-list" class="form-control">
                <option value="">Select</option>
                </select>
            </td>
        </tr>
    </table>
</form>
<script>
    function getdistrict(val) {
        $.ajax({
            type: "POST",
            url: "get_district.php",
            data: "state_id=" + val,
            success: function (data) {
                $("#district-list").html(data);
            },
        });
    }
</script>

get_district.php:

<?php
    require_once("config.php");
    if(!empty($_POST["state_id"])){
        $query =mysqli_query($con,"SELECT * FROM district WHERE StCode = '" . $_POST["state_id"] . "'");
?>
    <option value="">Select District</option>
    <?php
        while($row=mysqli_fetch_array($query)){
    ?>
        <option value="<?php echo $row["DistrictName"]; ?>"><?php echo $row["DistrictName"]; ?></option>
    <?php
        }
    }
?>
question from:https://stackoverflow.com/questions/65868312/dynamic-dropdown-with-auto-select-one-option-and-its-sub-dropdown-options

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would need to do 2 things

1.) insert a selected flag into the input

2.) activate the second dropdown on page load

1.) If the selected entry does never change then hardcode the default selected field in your code like if $var = XXX then echo "selected". If you have any function which calculates the default value e.g. a geolocation service like e.g. maxmind.com which takes the users remote IP address and outputs his current state, then use this result to set the selected entry.

<?php
while($row=mysqli_fetch_array($query)){          
$row["StCode"] == "DESIRED_VALUE" ? $selected ="selected" : $selected ="";
?>
 
<option value="<?php echo $row['StCode'];?>" <?= $selected ?> ><?php echo $row['StateName'];?>
</option>
  [...]    

Or add a field in the table SELECTED where you mark the default record with 1.

<?php
while($row=mysqli_fetch_array($query)){         
$row["selected"] == 1 ? $selected ="selected" : $selected ="";
?>
<option value="<?php echo $row['StCode'];?>" <?= $selected ?>  >
<?php echo $row['StateName'];?>
</option>
[...]

2.) You have to set the second select dropdown on page load. There are several ways to do this - this is explained here in detail: Jquery execute onchange event on onload


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...