使用 PHP 从 <option> 标签中获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6767748/
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-26 01:11:29  来源:igfitidea点击:

Get Text From <option> Tag Using PHP

phptextgetoption

提问by Abdul wahab

I want to get text inside the <option>tags as well as its value.

我想在<option>标签内获取文本及其值。

Example

例子

<select name="make">
<option value="5"> Text </option>
</select>

I used $_POST['make'];and I get the value 5but I want to get both value and the text.

我使用$_POST['make'];并获得了值,5但我想同时获得 value 和 text

How can I do it using PHP?

我怎样才能使用 PHP 做到这一点?

回答by serialworm

In order to get both the label and the value using just PHP, you need to have both arguments as part of the value.

为了仅使用 PHP 获取标签和值,您需要将这两个参数都作为值的一部分。

For example:

例如:

<select name="make">
    <option value="Text:5"> Text </option>
</select>

PHP Code

PHP代码

<?php
$parts = $_POST['make'];
$arr = explode(':', $parts);

print_r($arr);

Output:

输出:

Array(
  [0] => 'Text',
  [1] => 5
)

This is one way to do it.

这是一种方法。

回答by armandomiani

What about this? I think it's the best solution because you have separated fields to each data. Only one hidden field which is updated at each change and avoids hardcoding mappings.

那这个呢?我认为这是最好的解决方案,因为您将每个数据的字段分开。只有一个隐藏字段在每次更改时更新并避免硬编码映射。

This inside HTML:

这在 HTML 中:

<select name='make' onchange="setTextField(this)">
<option value = '' selected> None </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<input id="make_text" type = "hidden" name = "make_text" value = "" />
<script type="text/javascript">
    function setTextField(ddl) {
        document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
    }
</script>

This inside PHP:

PHP里面的这个:

<?php
$value = $_POST["make"];
$text = $_POST["make_text"];
?>

回答by thescientist

set the value of text to the value of the option tag, be it through static HTML markup or even if it's being generated by a server side script. You will only get the value attribute through POST

将 text 的值设置为 option 标签的值,无论是通过静态 HTML 标记还是由服务器端脚本生成。您只会通过 POST 获取 value 属性

Another option however, on the server side, is to map the value ("5"), to an associative array, i.e.

然而,在服务器端,另一种选择是将值(“5”)映射到关联数组,即

<?php
$valueTextMap = array("5" => "Text");

$value = $_POST['make'];  //equals 5
$text = $valueTextMap[$value];  //equals "Text"
?>

回答by Jonathan M

You'll need to include that Text in the value to begin with (e.g.: <option value="5_Text"> Text </option>and then parse, or...

您需要将该文本包含在要开始的值中(例如:<option value="5_Text"> Text </option>然后解析,或...

You could use javascript on the page to submit the text as another parm in the POST action.

您可以在页面上使用 javascript 将文本作为 POST 操作中的另一个参数提交。

回答by ProGrammer

I have always used a very elegant solution, similar to the ones already presented, which does not require a lot of additional code.

我一直使用一个非常优雅的解决方案,类似于已经提出的解决方案,它不需要很多额外的代码。

HTML

HTML

<select name="make">
  <option value="1:First Option">First Option Text</option>
  <option value="2:Second Option">Second Option Text</option>
  <option value="3:Third Option Text">Third Option Text</option>
</select>

PHP

PHP

$value = split(':', $make)[0];
$text = split(':', $make)[1];

Benefits of this method
Yes, there are definitely similarities to serialworm's answer, yet we minimize the code in our PHP block by inconspicuously converting to an array and picking the element required right away.

这种方法的好处
是的,肯定与 serialworm 的答案有相似之处,但我们通过不显眼地转换为数组并立即选择所需元素来最小化 PHP 块中的代码。

In my case, I use this exact short-hand code in a contact form where this one-liner (to get the selected department name) is critical to keeping the code looking clean.

在我的例子中,我在联系表格中使用这个确切的简写代码,其中这个单行(用于获取所选部门名称)对于保持代码看起来干净至关重要。