在mysqli中编辑记录时,在下拉列表中显示先前选择的值 [英] Display previously selected value in drop down when editing record in mysqli
问题描述
我继承了一个小php页面,该页面允许用户输入重定位请求,然后返回并对其进行编辑。
I have inherited a little php page that allows a user to enter relocation requests and then go back in and edit them.
添加页面上有很多下拉菜单下拉框,但编辑页面仅将其显示为文本字段。如果您需要在编辑页面中选择其他值,则需要了解下拉框中的所有值。
The add page has a number of drop down boxes, but the edit page just shows them as a text field. If you need to select a different value in the edit page you need to know all the values from the drop down box.
我正在尝试一种方法来在编辑页面上的下拉框中,从下拉框中显示先前选择的值,以方便他人根据需要更改值。
I am trying to come up with a way to show the previous selected value from the drop down box in a drop down box on the edit page to easily allow someone to change the value if need be.
我已经看到了一些其他问题和答案,例如使用选定的方法,但我无法将其与正在查看的代码相关联。我不是php方面的专家,因此在此使用示例和解决方案使事情正常进行。在这种情况下,我无法将它们放在一起。
I have seen some other questions and answers like this using the 'selected' method but I can't relate that to the code I am looking at. I am not great expert in php and use examples and solutions on here to make things work. In this instance I just cannot put it together.
感谢任何帮助。
谢谢
<?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>
推荐答案
工作代码:
您可以使用所选的选项。
假设您有两种形式:
添加形式。
更新表单。
首先通过GET方法发布url中的值,以便您可以访问另一个页面中的字段值。
You can use the selected option. Suppose if you have two forms: Add form. Update form. First post the values in url through the GET method so that you can access the field value in another page.
<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>
这篇关于在mysqli中编辑记录时,在下拉列表中显示先前选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!