PHP +表单选择选项
我有一个看起来像这样的选择,它是用 HTML 编写的,不通过任何 php 呈现,
<select name="position">
<option value="left">Left</option>
<option value="right">Right</option>
<option value="centre">Centre</option>
</select>
该值被发送到数据库,然后以 $v[ 形式的变量从数据库返回'position'],使用这个和我的原始形式如何使与变量匹配的选项成为默认选择?
I have a select that looks like this, it is written in in HTML and is not rendered via any php,
<select name="position">
<option value="left">Left</option>
<option value="right">Right</option>
<option value="centre">Centre</option>
</select>
The value gets sent to database and later on gets returned from the database as a variable in the form of $v['position']
, using this and my original form how can I make the option that matches the varibale the default selection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您没有指定表单何时再次显示。
如果用户提交表单后立即插入此代码片段到每个选项:
或者:
您必须通过 PHP 迭代变量:(
You didn't specify when the form displayed again.
If immediately when user is submitted the form, you need to insert this snippet to every option:
OR:
You must iterate through variables via PHP :(
您可以在循环中创建选项,并检查当前元素是否等于
$v['position']
中的值,并相应地设置selected
属性。You can create the options in a loop and check whether the current element equals the value in
$v['position']
and set theselected
attribute accordingly.您可以使用 DOM 来完成此操作,而无需接触 HTML。如果这是您的 HTML:
这是所选的值:
您可以这样做
这将输出:
You can do it with DOM without having to touch your HTML. If this is your HTML:
And this is the value that was selected:
You can do
And that will output:
试试这个
try this