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 for diafol

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.