Get text from <option> Tag using PHP

I want to get the 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 got the value 5 , but I want to get both the value and the text .

How to do it using PHP?

+11
php text get option
source share
6 answers

To get both a label and a value using only 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); 

Exit:

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

This is one way to do this.

+5
source share

How about this? I think this is the best solution because you divided the fields into each information. Only one hidden field that is updated with every change and avoids hardcoding mappings.

This is inside the 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 is inside PHP:

 <?php $value = $_POST["make"]; $text = $_POST["make_text"]; ?> 
+4
source share

set the text value to the value of the parameter tag, whether it is static HTML markup or even if it is created on the server side of the script. You will receive the value attribute only through POST

Another option, however, on the server side is to map the value ("5") to the associative array, that is

 <?php $valueTextMap = array("5" => "Text"); $value = $_POST['make']; //equals 5 $text = $valueTextMap[$value]; //equals "Text" ?> 
+3
source share

You will need to include this text in the value to start with (for example: <option value="5_Text"> Text </option> , and then parse or ...

You can use javascript on the page to send the text as another parm in the POST action.

+2
source share

I see that all of these answers use php split() which was removed in php 7.0.0 .

You can use explode() . It looks like a split. View here.

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

Php

 $parts = $_POST['make']; $arr = explode(':', $parts); print_r($arr); 
0
source share

I always used a very elegant solution, similar to those 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]; 

Advantages of this method
Yes, there is definitely a resemblance to the serialworm answer, but we minimize the code in our PHP block by quietly moving to the array and immediately select the element.

In my case, I use this exact short code in the form of a contact, where this one-line (to get the department name selected) is crucial to keep the code looking clean.

-one
source share

All Articles