0

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>      

3 Answers 3

3

Replace

echo '<option value="'.$key.'">'.$value.'</option>';

with

echo '<option>'.$value.'</option>';
Sign up to request clarification or add additional context in comments.

2 Comments

Why should I set the value attribute if the value gets automatically taken from the tag? Mapping the key to the value however wouldn't be a bad idea - You could at least save some bytes on network traffic.
Louis, your method worked. Jake, apologies, I'm not sure I understand what you're suggesting, but willing to learn.
3

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>

Comments

1

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;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.