[英]selected value get from db into Data list option using php mysql
I need to get selected value from db into datalist box.Tell me how to do it.我需要从 db 中获取选定的值到 datalist 框中。告诉我怎么做。 Here is the code.这是代码。
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
?>
<option value="<?php echo $row['Rank_Name'];?>"></option>
<?php
}
?>
</datalist>
If i understood right , you need to select value in dropdownlist with other value also.如果我理解正确,您还需要在下拉列表中选择具有其他值的值。 You can achieve this by doing this你可以通过这样做来实现这一点
<?php
$select1="select Rank_Name from ranks where Rank_Status=1";
$q=mysqli_query($select1) or die($select1);
$row=mysqli_fetch_array($q); //here you are getting name of person whose rank is 1
?>
<datalist id="Rank_Name">
<?php
$s="select * from ranks ";
$q=mysqli_query($s) or die($s);
while($r=mysqli_fetch_array($q))
{ ?>
<option value="<?php echo $r['Rank_Name']; ?>"<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>>
<?php echo $r['Rank_Name']; ?>
</option>
<?php } ?>
</datalist>
In above code, this line <?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>
在上面的代码中,这一行<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>
<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>
check if value are same from first query ,and if same then that option will be get selected
automatically <?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>
检查第一次查询的值是否相同,如果相同则该选项将被自动selected
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
echo "<option value=".$row['Rank_Name']."></option>";
}
?>
</datalist>
try this code.试试这个代码。 im using echo <option>
with while loop我在 while 循环中使用 echo <option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.