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>

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

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

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