我继承了一个允许用户输入重定位请求然后重新编辑的PHP页面。
添加页面有许多下拉框,但是编辑页面只是将它们显示为文本字段。如果您需要在编辑页面中选择其他值,则需要从下拉框中了解所有值。
我试图提出一种从编辑页面上的下拉框中的下拉框显示上一个选定的值的方法
我使用"选定"方法看到了其他一些问题和答案,但我无法将其与我正在查看的代码联系起来。我不是PHP方面的出色专家,并且在这里使用示例和解决方案来使事情起作用。在这种情况下,我只是无法将其放在一起。
感谢任何帮助。
谢谢
<?php
$resultNames = $conn->query("SELECT txtCraftGroup FROM tblCraftGroup Order
by txtCraftGroup");
if($resultNames) {
?>
<tr>
<td>Craft: </td>
<td><select name="craft">
<option value="0">Please Select</option>
<?php
while ($MCraft = $resultNames->fetch_assoc()) {
echo "<option value="{$MCraft['txtCraftGroup']}">";
echo $MCraft['txtCraftGroup'];
echo "</option>";
}
}else{
?>
<td>Craft: </td>
<td><input type="text" name="craft" value="<?php echo $MCraft; ?>"/><br/>
</td>
<?php
}
?>
</select><td>
</tr>
To display the selected option in dropdown
<td><select name="craft">
<?php
while ($MCraft = $resultNames->fetch_assoc())
{ ?>
<option value="<?php echo $MCraft['txtCraftGroup'] ;?>"
<?php if ($MCraft['txtCraftGroup']==$craftval) echo 'selected = "selected"'; ?> >
<?php echo $MCraft['txtCraftGroup']; ?></option>
<?php
}
?>
</select> </td>
例如
<td><select name="gender">
<?php
while ($row = $resultNames->fetch_assoc())
{ ?>
<option value="male"
<?php if ($row['gender']=="male") echo 'selected = "selected" '; ?> >
Male
</option>
<option value="female"
<?php if ($row['gender']=="female") echo 'selected = "selected" '; ?> >
Female
</option>
<?php
}
?>
</select> </td>
工作代码:
您可以使用所选选项。假设您有两种形式: 添加表单。 更新表格。首先通过get方法发布URL中的值,以便您可以在另一页中访问字段值。
<select name = "field_name">
<option value="<?php echo $_GET["field_name"]; ?>" selected > <?php echo
$_GET["field_name"]; ?> </option>
{
//you can add your dropdown condition here if you are fetching an array
}
</select>