[英]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. 单击“编辑”按钮后,我将重定向到另一个页面editvendor.php,其中我要编辑所有参数,但是在此页面中,我还具有2个相互依赖的下拉列表,通过该列表我可以将每个供应商归类为特定类别,并且子类别。 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 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 id供应商名称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. 对于与供应商相关联的option
标签,添加一个selected
参数以在选择列表中对其进行预选择。
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: 为此,您需要知道供应商实际属于哪个类别,我找不到任何与之相关的代码,因此仅举一个示例和一些猜测:您将获得供应商实际所属的类别ID,并在其中您检查的所有类别的while循环,如果id与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>
<?}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.