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
This is not something that can be done through PHP alone. The PHP script can only “see” the information which is posted (the
valuefor theselectedoption that is posted). You can use javascript to alter a hiddeninputfield with the text contents of a selectedoption, and this will be included in the$_POSTarray:This will make the
$_POST['test_text']available with the selected index (but you should also force theonchange()function when the page loads so that it will be populated even if the user leaves the select field at the default value.