[英]pre select a value from dropdown based on link clicked in core php
need help. 需要帮忙。
when i click the link on page 1 the value should be pre selected in a drop down on page 2. how should i do it in core php? 当我单击第1页上的链接时,应该在第2页上的下拉列表中预先选择值。我该如何在核心php中进行操作? the drop down is listed from a database 下拉列表是从数据库中列出的
Page1. 第1页。
<td>
<a href="assign_student.php" class="link">Assign Student</a>
</td>
Page2 第2页
<div class="form-group">
<label for="classes">Select Class </label>
<select name="classes" class="form-control">
<?php foreach ($resultClass as $row): ?>
<option value="<?php echo $row['id']?>"><?php echo $row["class_name"]?>
</option>
<?php endforeach ?>
You can do 你可以做
<?php foreach ($resultClass as $row):
if($row['id'] == 'Your desired id'){
?>
<option value="<?php echo $row['id']?>" selected><?php echo $row["class_name"]?>
</option>
<?php } else{
<option value="<?php echo $row['id']?>"><?php echo $row["class_name"]?>
</option>
<?php }endforeach ?>
Pass value in query string in link for example 例如,在链接的查询字符串中传递值
<a href="assign_student.php?class=2" class="link">Assign Student</a>
then you will get that variable on Page 2 by 那么您将在第2页上获得该变量
$_GET['class']
compare it in select > options like 在选择>选项中进行比较
<option value="<?php echo $row['id']?>" <?php if($row['id'] == $_GET['class']) { echo "selected='selected'"; } ?> ><?php echo $row["class_name"]?>
Best luck! 好运! :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.