4

in the following:

<form  action="test.php"  method="POST">  
    <select id="test" name="test">
     <option value="1">Test One</option>
     <option value="2">Test Two</option>
    </select>
</form>

in test.php, I can get 1 or 2 as follow:

$result=$_POST['test'];

How can I get the text of the selected option (i.e. "Test One" or "Test Two") using php

Ben D
  • 14,321
  • 3
  • 45
  • 59
Max_Salah
  • 2,407
  • 11
  • 39
  • 68
  • If you want the text, why not make the text the value? – ಠ_ಠ Aug 13 '12 at 00:24
  • Would you mind naming the specifics of the situation (ie Why you need both)? Maybe we can brainstorm a solution. – ಠ_ಠ Aug 13 '12 at 00:26
  • If you want to use the 1 and 2 value in JavaScript you can assign it to a different attribute such as title, alt, or anything you want. – Stephen Cioffi Aug 13 '12 at 00:30
  • I assume it's a typo, but your `select` element is missing its (mandatory) `name` attribute, which should be `id` (after your update it should be `test`). – powerbuoy Aug 13 '12 at 00:30
  • We can give you all the possibilities you want, but until you tell us what you are truly trying to accomplish by passing both values, nobody can give you the best answer possible. – ಠ_ಠ Aug 13 '12 at 00:36
  • @powerbuoy - Good catch. If the user is able to access it in the POST array he must have just missed it when typing up the question (i've updated it). – Ben D Aug 13 '12 at 01:11

9 Answers9

23

This is not something that can be done through PHP alone. The PHP script can only "see" the information which is posted (the value for the selected option that is posted). You can use javascript to alter a hidden input field with the text contents of a selected option, and this will be included in the $_POST array:

<form  action="test.php"  method="POST">  
    <select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
     <option value="1">Test One</option>
     <option value="2">Test Two</option>
    </select>

<input type="hidden" name="test_text" id="text_content" value="" />
</form>

This will make the $_POST['test_text'] available with the selected index (but you should also force the onchange() function when the page loads so that it will be populated even if the user leaves the select field at the default value.

Ben D
  • 14,321
  • 3
  • 45
  • 59
16

Only the value of the form control will be sent to the server. An easy way to send both is to simply include both in the the value:

<select name="test">
    <option value="1|Test one">Test one</option>
    <option value="2|Test two">Test two</option>
</select>

And then:

$test = explode('|', $_POST['test']);

Then you'll end up with $test[0] being "1" and $test[1] being "Test one".

powerbuoy
  • 12,460
  • 7
  • 48
  • 78
3

You can't; that information is not sent back to the server. You will need to look at how you generated the HTML in the first place and get the text from there.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

It is not sent so the only way to get it is having an array mapping values to titles in your PHP code.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

This is a solution I could use except I already have the onChange calling a function to show a hidden block based on the selected building type in a statement.

I have building types I need the user to select. Several building types have a unique set of questions, e.g. Bank Branch, Data Center, Courtroom, etc. But there are many that have the same set of questions which I call Other Type. Each option has their associated value, e.g. Bank Branch has a value of "Bank_Branch", Data Center has "Data_Center", but Other Type has a value of "Other_Type". The text for "Other_Type" differs based on the building type, such as Convention Center, Museum, Performing Arts, etc. So I need the value "Other_Type" to "show" the questions in the "Other_Type" DIV block while using the text value to send in an email identifying the type of building, e.g. Convention Center, Museum, Performing Arts, etc.

Any way to use PHP to get the text value of the selected item? I'm already using the following inside the HTML

   var sele = document.getElementById('building_type');
   var seleVal = sele.options[sele.selectedIndex].value;
   var seleTxt = sele.options[sele.selectedIndex].text;
   document.getElementById("other_text").innerHTML = seleTxt;

I'm not seeing a way to do this.

SOLVED: I can simply create the hidden div and in my check for Other Type set the innerHTML for the hidden div.

  • I spoke too soon: I added: Then set document.getElementById("hidden_text").value = seleTxt; If I make the "hidden_text" filed visible the value I want is there. But when I try to use it, $_POST['hidden_text'] is empty. Any ideas why $_POST['hidden_text'] would be empty? –  Oct 14 '14 at 21:58
0

The text for the selected option is not passed through the HTML form. If you want the text, you have to store that in a hidden HTML input on your page, or you need to add some business logic in your PHP code to translate the value of ìd into the text (through a switch statement, or by querying a database, etc.)

scott.korin
  • 2,537
  • 2
  • 23
  • 36
0

Unfortunately when you submit a form and a variable it only takes one parameter which is it's value. You would need to make the value. Test One the value in order for it to pass on to the PHP script. What is the purpose of value="1" cause you can probably use it in a different attribute?

Stephen Cioffi
  • 1,161
  • 2
  • 13
  • 32
0

Im not a experienced php programer, but u can check the value selected in SELECT tag with php. i dont know why people say u can not.

if ( $_POST['test'] == 1) 
{ do something } 
else 
{ option value 2 is selected, do something else }

Im sure this will work.

Gngogh
  • 177
  • 2
  • 8
0

Try it if 1, 2 not needed. you will get required text as value

<form  action="test.php"  method="POST">  
    <select id="test" name="test">
     <option value="Test One">Test One</option>
     <option value="Test Two">Test Two</option>
    </select>
</form>
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89