Start New Discussion within our Web Development Community

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']);}?>" />

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;?>
This article has been dead for over six months. Start a new discussion instead.