How to post two values in an option field?
Solution 1:
You cannot post two values unless both of them appear in the value
attribute. You can place both in, separated by a comma or hyphen and explode()
them apart in PHP:
// Place both values into the value attribute, separated by "-"
<option value="<?php echo $name['id'] . "-" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
Receiving them in PHP
// split the contents of $_POST['data'] on a hyphen, returning at most two items
list($data_id, $data_name) = explode("-", $_POST['data'], 2);
echo "id: $data_id, name: $data_name";
Solution 2:
You may add a hidden field with the name "id" and then bind an onchange event listener to the <select>
. inside the onchange function, get the value of the <select>
and assign it to the "id" field.
<form>
<select name="name" onchange="document.getElementById('id').value=this.value">
<!--
...
options
...
-->
</select>
<input type="hidden" name="id" id="id" />
<input type="submit" />
</form>