简体   繁体   中英

Display predefined value in dropdown list

i have a list of vendors where i wish to edit them. on the click of edit button, i get redirected to another page editvendor.php where i have all the parametrs that i wish to edit, but in this page i also have 2 interdependent dropdown list through which i can categorize each vendor under scpecific category and subcategory. the code for the dropdown is

<script>
$(document).ready(function(){
    $('#cat').change(function(){
        var catid = $('#cat').val();

        if(catid != 0)
        {
            $.ajax({
                type:'post',
                url:'fetchsubcat2.php',
                data:{id:catid},
                cache:false,
                success: function(returndata){
                    $('#subcat').html(returndata);
                }
            });
        }
    })
})
</script>

<fieldset>
    <label>Category</label>
    <select name="catid" id="cat" >
        <option value=""> Please select a category </option>
        <?php
                $sql = "SELECT * FROM category";
                $result = mysqli_query($con, $sql);

                if (mysqli_num_rows($result) > 0) {
                    while($row = mysqli_fetch_assoc($result)) 
                        {
                            $catname=$row["category"];
                            $catid=$row["id"];
        ?>
        <option value="<? echo $catid;?>"><? echo $catname;?></option>
                        <?}
                }?>
    </select>
</fieldset> 

<fieldset>
    <label>Sub Category</label>
    <select name="subcatid" id="subcat" >
        <option></option>
    </select>
</fieldset> 

Code for fetchsubcat2.php

<?php
    require 'connection.php';
    $catid =  $_REQUEST['id']; 

        $sql = "SELECT * FROM subcategory where catid='".$catid."'";
        $result = mysqli_query($con, $sql);

        if (mysqli_num_rows($result) > 0) 
            {
                ?><option value="">Select a subcategory</option><?
                while($row = mysqli_fetch_assoc($result)) 
                    {
                        $subcatname=$row["subcatname"];
                        $subcatid=$row["id"];
?>

<option value="<? echo $subcatid;?>"><? echo $subcatname;?></option>
                  <?}
            }
        else
            {?>
                <option value="">No sub category </option>
            <?}?>

Now what i want is that if the vendor is already under a category and subcategory then in place of "Please select a category" and "Select a subcategory" that existing category and subcategory should get be displayed. As shown below, in place of this

在此处输入图片说明

i want

在此处输入图片说明

After that if i want to change the category/subcategory i can do that by clicking on the dropdown list.

Table view

Vendor table

id vendorname catid catname subcatid subcatname 1 ABC 1 C1 3 S3

For the option tag, which is the one associated with a vendor, add a selected parameter to pre-select it in the select list.

For that you need to know, which category a vendor actually belongs to, i can't find any code referring to that, so just an example and a bit of guesswork: You get the id of the category the vendor actually belongs to and in the while loop of all of your categories you check, if the id matches the vendor-catid:

<?php
while($row = mysqli_fetch_assoc($result)) {
    $catname=$row["category"];
    $catid=$row["id"];
    ?>
    <option value="<? echo $catid;?>" <?php echo ( $catid === $vendorcatid ? 'selected' : ''); ?>><? echo $catname;?></option>
<?}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM