I have a combo box named "Make". In that combo box I'm loading vehicle manufacturer names. When I click SEARCH button I want to display the selected manufacturer name. Below is part of my HTML code.
<label for="Manufacturer"> Manufacturer : </label>
<select id="cmbMake" name="Make" >
<option value="0">Select Manufacturer</option>
<option value="1">--Any--</option>
<option value="2">Toyota</option>
<option value="3">Nissan</option>
</select>
<input type="submit" name="search" value="Search"/>
Below is my PHP code so far I've done.
<?php
if(isset($_POST['search']))
{
$maker = mysql_real_escape_string($_POST['Make']);
echo $maker;
}
?>
If I select Toyota from the combo box and press SEARCH button, I'm getting the answer as '2' . It means it gives me the value of the 'Toyota'. But I want to display the name 'Toyota'. How can I do that? Please help me ....
Change your select box options value:
You cann't get the text of selected option in php. it will give only the value of selected option.
EDITED:
ON php file:
Try with this. You will get the select box value in $_POST['Make'] and name will get in $_POST['selected_text']
if you fetching it from database then
you can make a jQuery onChange event to get the text from the combobox when the user select one of them:
When you select an option, it will save the text in an Input hidde
After that, when you submit the form, just catch up the value of the input
Then you can do wathever you want with the text, for instance save it in a session variable and send it to other page. etc.
You can achive this with creating new array:
Put whatever you want to send to PHP in the
value
attribute.You can also omit the
value
attribute. It defaults to using the text.If you don't want to change the HTML, you can put an array in your PHP to translate the values: