PHP获取下拉列表选择选项值
在我的下拉列表中,每个选项都有两个不同的值。我怎样才能检索两者?让我来说明一下我的意思。
<select name="my_ddl">
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
</select>
发布表单后,我希望能够获取所选选项的 $value_id 和 $value_text。我该怎么做?
$_POST['my_ddl'] 只能获取一个值,不能同时获取两个值。
在 asp.net 中,我可以简单地通过引用 my_ddl.Value 和 my_ddl.Text 来完成此操作。
谢谢你!
In my dropdownlist I have two different values for each option. How can I retrieve both? Let me illustrate what I mean.
<select name="my_ddl">
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
</select>
When the form is posted, I want to be able to get both the $value_id and $value_text of the selected option. How can I do this?
$_POST['my_ddl'] only gets one value not both.
In asp.net I could do this simply by referring to my_ddl.Value and my_ddl.Text.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
严格来说,这是不可能的。
您可以做的是在
value
属性中使用分隔符:并且...
Strictly, this is not possible.
What you could do is use a delimiter in your
value
attribute:And...
另一种奇怪的做法是:
然后当你处理它时,你可以这样做,甚至可以做一些更简单的事情:
因为 php 将 [] 视为意味着字符串是一个数组,所以你立即拥有一个关联数组。我同意,如果你首先把它放在那里,你应该能够在代码中再次查找它,而不是依赖于此。
Another strange way of doing it is:
Then when you process it you can do this or maybe even something more simple:
Because php treats the [] as meaning the string is an array and so you instantly have an associative array. I agree though that if you put it there in the first place you ought to be able to just look it up again in the code rather than rely on this.
如果您使用 PHP 填充
的文本,那么您可能只需在服务器上查找该值即可。也许您只需要使用 $value_id 来查找数据库表中的文本?
如果没有,您可以在表单中包含一个隐藏字段,并在每次选择新值时使用 JavaScript 用文本更新该隐藏字段。
If you're using PHP to populate the text for the
<option>
then you can probably just look the value up on the server. Perhaps you just need to use the $value_id to look up the text in a database table?If not, you could include a hidden field on your form and use JavaScript to update that hidden field with the text every time a new value is selected.
您无法从 POST 数据中获取 value_text。一种解决方案是通过 JavaScript 选择选项后填充隐藏字段。
You cannot get value_text from POST data. One solution is to populate the hidden field after choosing the option via JavaScript.