Get Text From <option> Tag Using PHP
Asked Answered
B

6

17

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.

How can I do it using PHP?

Backstairs answered 20/7, 2011 at 20:0 Comment(0)
D
16

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

For example:

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

PHP Code

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

print_r($arr);

Output:

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

This is one way to do it.

Dissident answered 20/7, 2011 at 20:7 Comment(2)
for all the responses regarding this matter, i think this is the most elegant one. will go this wayFought
I love this. I learned something new today. Thank you. I was needing both values to be submitted with a form, and this was perfect.Pinelli
A
5

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:

<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
$value = $_POST["make"];
$text = $_POST["make_text"];
?>
Agamemnon answered 20/7, 2011 at 20:28 Comment(1)
thanks for this line document.getElementById('make_text').value=ddl.options[ddl.selectedIndex].text;Wolverine
C
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

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

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

$value = $_POST['make'];  //equals 5
$text = $valueTextMap[$value];  //equals "Text"
?>
Calbert answered 20/7, 2011 at 20:2 Comment(0)
B
2

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

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

Brenn answered 20/7, 2011 at 20:4 Comment(0)
K
0

You can use simple dom

<?php 
include('/mnt/sdb1/addons/simplehtmldom/simple_html_dom.php');
$html = file_get_html('tari.html');
$opt = array();
foreach($html->find('option') as $a) {
 $opt[] = $a->value;
}
print_r($opt);
?>
Kb answered 26/3, 2022 at 15:41 Comment(0)
B
-1

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

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

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

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.

Bellboy answered 19/2, 2018 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.