Reputation: 742
The select tag is being generated from the database through PHP echo command.
After I post the form including my select element, I want to be able to retrieve the content (between option tag) as well as the value in the option tag. So if I chose Volvo and submitted the form. I want to retrieve Volvo and 1.
<select name='car'>
<option value='1'>Volvo</option>
<option value='2>Saab</option>
<option value='3>Mercede</option>
<option value='4>Audi</option>
</select>
This will only retrieve the value.
$_POST['car']
how can I retrieve both?
Upvotes: 0
Views: 272
Reputation: 1894
you can get like this
<select name='car'>
<option value='1,Volvo'>Volvo</option>
</select>
and
$value = explode(",",$_POST['car']);
you get
$value[0]= 1
$value[1]= Volvo
Upvotes: 1
Reputation: 2140
Unless you're using jQuery to manipulate your submission, you can just modify the value
attribute to include the same word as the content of the option. You wouldn't be able to submit POST data extracted from the content of the option, though.
Upvotes: 1
Reputation: 23563
You can't. But you know that 1 is always Volvo.
Just keep it in an array:
$cars = array('Volvo', 'Saab,' 'Mercede', 'Audi');
$car_name = $cars[$_POST['car']];
This way you can also generate the select from the array:
<select name='car'>
<? foreach ($cars as $key => $car_name): ?>
<option value='<?= $key ?>'><?= $car_name ?></option>
<? endforeach; ?>
</select>
Upvotes: 2