1,105,375 Community Members

how to save value from drop menu by using php.

Member Avatar
hwoarang69
Posting Pro
572 posts since Feb 2012
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

how to save value from drop menu by using php.

<select name = 'color'>
    <option value='black'>Black</option>
    <option value='gray'>Gray</option>
    <option value='white'>White</option>
</select>

i am saving the value from text field by doing this. i am thinking if i can do some thing like this in drop down menu.

<input type="text" name="username"  
   value="<?php if(isset($_POST['username'])){echo htmlentities($_POST['username']);}?>" />
Member Avatar
diafol
Where are my eyes?
12,982 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
1
 

One approach:

$options = '';
$select_array = array('black', 'gray', 'white');
$sel_color = (isset($_POST['color']) && in_array($_POST['color'], $select_array)) ? $_POST['color'] : '';
foreach($select_array as $color){
    $sel = ($sel_color == $color) : ' selected="selected"' : '';
    $options .= "<option value='color'$sel>" . strtoupper($color) . "</option>"; 
}
...
<select name="color">
    <?php echo $options;?>
</select>
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article