Skip to content Skip to sidebar Skip to footer

How To Show A Drop-down List With A Pre-selected Option

I want to update my form when I click edit button then all info is showing correctly but status value is showing all time same open option. I dont know why it is showing same open

Solution 1:

Use selected attribute.

<select name="status" id="status">
    <option value="open" <?php if($status=="open") { echo "selected"; } ?> >Open</option>
    <option value="done" <?php if($status=="done") { echo "selected"; } ?> >Done</option>
    <option value="pending" <?php if($status=="pending") { echo "selected"; } ?> >Pending</option>
    <option value="working" <?php if($status=="working") { echo "selected"; } ?> >Working</option>
</select>

Solution 2:

This is wrong way, correct way is to use like this,

<select name="status">
<?php
$options = array("open","done","pending","working");
$selected = "done";
foreach($options as $option){
    if($selected==$option){
        echo '<option value="'.$option.'" selected="selected">'.ucfirst($option).'</option>';
    }else{
        echo '<option value="'.$option.'">'.ucfirst($option).'</option>';
    }
}
?>
    </select>

Solution 3:

If you really want to embed the status in the options tag, you should use the 'selected' attribute for selection of the tag options. Here is your modified code with correct handling:-

<p><label class="field" for="username">UserName:</label>
 <input name="username" type="text" id="username"  value="<?php echo $username;?>"       size="50" />
 </p>
 <p>

            <label class="field" for="Status">Status</label>
                <select name="status" id="status" >
                    <option value="open" <?php echo $status == 'open' ? 'selected' : ''; ?>>Open</option>
                    <option value="done" <?php echo $status == 'done' ? 'selected' : '' ;?>>Done</option>
                    <option value="pending" <?php echo $status == 'pending' ? 'selected' : '' ; ?>>Pending</option>
                    <option value="working" <?php echo $status == 'working' ? 'selected' : '' ; ?>>Working</option>
                </select>
   </p>

Solution 4:

You need selected tag. Modify your form like this

<select name="status" id="status"> <!-- value removed, there's no use of it here -->
    <option value="open" <?php if ($status=="open") {echo "selected"}?>>Open</option>
    <option value="done" <?php if ($status=="done") {echo "selected"}?>>Done</option>
    ....
</select>

Solution 5:

Use selected attribute for option.

Change your code as

<select name="status" id="status">
  <option value="open" <?php selected($status, 'open'); ?>>Open</option>
  <option value="done" <?php selected($status, 'done'); ?>>Done</option>
  <option value="pending" <?php selected($status, 'pending'); ?>>Pending</option>
  <option value="working" <?php selected($status, 'working'); ?>>Working</option>
</select>

<?php
function selected( $selected_value, $value ) {
   if( $selected_value === $value ) {
      echo 'selected=true';
   }
}
?>

Post a Comment for "How To Show A Drop-down List With A Pre-selected Option"