使用PHP从标签获取文本

时间:2022-06-09 09:56:54

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

我想在

Example

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

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

我用$ _POST ['make'];我得到值5,但我想得到价值和文本。

How can I do it using PHP?

我怎么能用PHP做到这一点?

5 个解决方案

#1


4  

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"];
?>

#2


3  

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 = split(':', $parts);

print_r($arr);

Output:

输出:

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

This is one way to do it.

这是一种方法。

#3


3  

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"
?>

#4


1  

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...

您需要在值中包含该Text以开始(例如:

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

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

#5


0  

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.

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

#1


4  

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"];
?>

#2


3  

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 = split(':', $parts);

print_r($arr);

Output:

输出:

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

This is one way to do it.

这是一种方法。

#3


3  

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"
?>

#4


1  

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...

您需要在值中包含该Text以开始(例如:

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

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

#5


0  

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.

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