PHP 获取下拉列表选择选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4420747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:01:25  来源:igfitidea点击:

PHP get dropdownlist select option value

phpdrop-down-menu

提问by

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?

当表单发布时,我希望能够同时获得所选选项的 $value_id 和 $value_text。我怎样才能做到这一点?

$_POST['my_ddl'] only gets one value not both.

$_POST['my_ddl'] 只得到一个值,而不是两个。

In asp.net I could do this simply by referring to my_ddl.Value and my_ddl.Text.

在asp.net 中,我可以简单地通过引用my_ddl.Value 和my_ddl.Text 来做到这一点。

Thank you!

谢谢!

采纳答案by Jacob Relkin

Strictly, this is not possible.

严格来说,这是不可能的。

What you could dois use a delimiter in your valueattribute:

可以做的是在您的value属性中使用分隔符:

<select name="my_ddl">
  <option value="<?php echo $value_Id ?>|<?php echo $value_text ?>"><?php echo $value_text ?>   
  </option>
</select>

And...

和...

<?php    
   list($id, $text) = explode('|', $_POST['my_ddl']);
   //...
?>

回答by Alexio

Another strange way of doing it is:

另一种奇怪的做法是:

<select name="my_ddl">
  <option value="<?php echo $value_Id ?>[<?php echo $value_text ?>]">
    <?php echo $value_text ?>   
  </option>
</select>

Then when you process it you can do this or maybe even something more simple:

然后当你处理它时,你可以这样做,或者甚至更简单:

foreach ($_POST['my_dd1'] as $value_Id => $value_text) {
  $value_Id = $value_Id;
  $value_text = $value_text;
}

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 将 [] 视为表示字符串是一个数组,所以您立即拥有一个关联数组。我同意,如果你把它放在第一位,你应该能够在代码中再次查找它而不是依赖于此。

回答by AgentConundrum

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?

如果您使用 PHP 来填充文本,<option>那么您可能只需在服务器上查找该值即可。也许您只需要使用 $value_id 来查找数据库表中的文本?

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.

如果没有,您可以在表单中包含一个隐藏字段,并在每次选择新值时使用 JavaScript 用文本更新该隐藏字段。

回答by Sergei

You cannot get value_text from POST data. One solution is to populate the hidden field after choosing the option via JavaScript.

您无法从 POST 数据中获取 value_text。一种解决方案是在通过 JavaScript 选择选项后填充隐藏字段。