Reputation: 1197
My form is working fine but in my results email I get the number of the array the user chose instead of the value (ex. Glove: 3 vs. Glove: Cherry). How can I change this?
This is the php I'm using for my form:
$unpwdgloves=array(
'Bubble Gum',
'Cherry',
'Green Apple',
'Vanilla Orange',
'Grape',
'Mint',
);
This is my form:
<select name="glove-color">
<option value="">-----------------</option>
<?php
foreach($unpwdgloves as $key => $value):
echo '<option value="'.$key.'">'.$value.'</option>';
endforeach;
?>
</select>
Upvotes: 0
Views: 92
Reputation: 1829
You recieve the position of the selection because by default, the keys of an array are from 0->n. In your case:
$unpwdgloves=array(
[0] => 'Bubble Gum',
[1] => 'Cherry',
[2] => 'Green Apple',
[3] => 'Vanilla Orange',
[4] => 'Grape',
[5] => 'Mint',
);
So your select will look like:
<select name="glove-color">
<option value="0">Bubble Gum</option>
<option value="1">Cherry</option>
[...]
</select>
So the value passed in the POST will be the value in the "value" attribute. If you don't specify it's value attribute, by default the value sent to server is the text inside option tag <option>VALUE</option>
.
You can change your code to
echo '<option value="'.$value.'">'.$value.'</option>';
//<option value="Bubble Gum">Bubble Gum</option>
OR
echo '<option>'.$value.'</option>';
//<option>Bubble Gum</option>
Upvotes: 3
Reputation: 1110
Just make the following changes as key represents the index of the array you are using in the code.
<?php
foreach($unpwdgloves as $value):
echo '<option value="'.$value.'">'.$value.'</option>';
endforeach;
?>
Upvotes: 1
Reputation: 3714
Replace
echo '<option value="'.$key.'">'.$value.'</option>';
with
echo '<option>'.$value.'</option>';
Upvotes: 3