get the text of the selected option using php [duplicate]

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

Answers:

Answer

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.

Answer

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

Answer

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.

Answer

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

Answer

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.

Answer

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

Answer

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?

Answer

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.

Answer

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>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.