php - Display predefined value in dropdown list -
i have list of vendors wish edit them. on click of edit button, redirected page editvendor.php have parametrs wish edit, in page have 2 interdependent dropdown list through can categorize each vendor under scpecific category , subcategory. code 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 category </option> <?php $sql = "select * 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 fetchsubcat2.php
<?php require 'connection.php'; $catid = $_request['id']; $sql = "select * subcategory catid='".$catid."'"; $result = mysqli_query($con, $sql); if (mysqli_num_rows($result) > 0) { ?><option value="">select 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 want if vendor under category , subcategory in place of "please select category" , "select subcategory" existing category , subcategory should displayed. shown below, in place of
i want
after if want change category/subcategory can clicking on dropdown list.
table view
vendor table
id vendorname catid catname subcatid subcatname 1 abc 1 c1 3 s3
for option
tag, 1 associated vendor, add selected
parameter pre-select in select list.
for need know, category vendor belongs to, can't find code referring that, example , bit of guesswork: id of category vendor belongs , in while loop of of categories check, if id matches 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> <?}
Comments
Post a Comment