PHP 如何保持下拉列表中选定的选项在提交时保持选中状态?
我有:
<select name="topic" style="margin-bottom:3px;">
<option>General Question</option>
<option>Company Information</option>
<option>Customer Issue</option>
<option>Supplier Issue</option>
<option>Request For Quote</option>
<option>Other</option>
</select>
用于下拉。当提交表单时,它会进入验证页面。如果有错误,表单会保留用户输入的原始内容。我让它适用于所有输入字段和文本区域,但我如何通过下拉菜单来做到这一点?
我通过使用以下方式保留输入字段:
$name = $_REQUEST["name"];
在再次显示的表单中,有(忽略它在表格中的事实):
<tr>
<td>Name:*</td>
</tr>
<tr>
<td><input name="name" type="text" size="15" value="<?php echo $name ?>" maxlength="200" /></td>
</tr>
那么,对于下拉菜单有什么想法吗?
I have:
<select name="topic" style="margin-bottom:3px;">
<option>General Question</option>
<option>Company Information</option>
<option>Customer Issue</option>
<option>Supplier Issue</option>
<option>Request For Quote</option>
<option>Other</option>
</select>
for the drop down. And when the form is submitted, It goes to a validation page. If it has errors the form keeps the original content the user put in. I have it working for all of the input fields and textarea's, but how could I do this with a drop down?
I have the input fields staying by using:
$name = $_REQUEST["name"];
and in the form that shows up again, there is (ignore the fact that it is in a table):
<tr>
<td>Name:*</td>
</tr>
<tr>
<td><input name="name" type="text" size="15" value="<?php echo $name ?>" maxlength="200" /></td>
</tr>
So, any ideas for drop downs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将“selected”属性添加到适当的选项。我相信您还需要指定每个选项的值属性。我不确切知道你是如何生成该列表的,但这也许会有所帮助:
You need to add the "selected" attribute to the appropriate option. I believe you also need to specify the value attribute for each option. I don't know exactly how you are generating that list, but maybe this will help:
首先,为选项元素赋予一个 value 属性。这使得代码更加健壮,因为如果您决定更改选项的文本,它不会中断。在那之后:
First of all, give the option element a value attribute. This makes the code more robust, because it does not break should you decide to alter the text of an option. After that: