Freddy
Freddy

Reputation: 857

How to have a value already selected from <select> tag

I have the following dropdown implemented:

<select style="margin-left:25px;">
     <option value="null"> </option>
     <option value="single">Single</option>
     <option value="in_a_relationship">In a relationship</option>
     <option value="engaged">Engaged</option>
     <option value="married">Married</option>
     <option value="open_rel">In an open relationship</option>
     <option value="divorced">Divorced</option>
 </select>

Say for example, a user has already selected Single from the drop down menu. When they do back to this page, I want single to be already chosen from the list. For text fields I can simply get the value from the database and then assign it to a variable and then echo it in the value. I.e.

$get_data2 = mysqli_query ($connect, "SELECT * FROM user_bio WHERE username = '$username'");
        $row3 = mysqli_fetch_assoc ($get_data2);
                $bio = $row3['about_me'];

and then do ...

<textarea name="biotextarea" form="change_details" rows="4" cols="60" maxlength="255"><?php echo $bio; ?> </textarea>

How can I do this for a select tag?

Upvotes: 2

Views: 2382

Answers (3)

John Slegers
John Slegers

Reputation: 47081

Here's how to select the option Single in HTML :

<select style="margin-left:25px;">
     <option value="null"> </option>
     <option value="single" selected>Single</option>
     <option value="in_a_relationship">In a relationship</option>
     <option value="engaged">Engaged</option>
     <option value="married">Married</option>
     <option value="open_rel">In an open relationship</option>
     <option value="divorced">Divorced</option>
 </select>

So, how to set this in PHP?!

Well, you could generate your <select> element like this :

<?php
$options = [
    'null' => ' ',
    'single' => Single',
    'in_a_relationship' => 'In a relationship',
    'engaged' => 'Engaged',
    'married' => 'Married',
    'open_rel' => 'In an open relationship',
    'divorced' => 'Divorced'
];
?>
<select style="margin-left:25px;"><?php
     foreach($options as $key => $value) {
          echo '<option value="' . $key . '"';
          if ($key === $row3['status']){
              echo ' selected';
          }
          echo '>' . $value . '</option>';
     }
?></select>

Upvotes: 1

Mauricio Florez
Mauricio Florez

Reputation: 1132

The solution of Musa it's OK, but If you don't want to change your PHP too much you can do the task with Javascript.

With jQuery or pure Javascript

function selector(id, value){
    select = document.getElementById(id);
  for(i=0;i<select.options.length;i++){
    if(select.options[i].value == value){
        select.selectedIndex = i;
    }
  }

}
//Pure Javascript
selector("status", "engaged")

//With jQuery
jQuery('#status').val('married');

Check this Snippet

Upvotes: 0

Musa
Musa

Reputation: 97672

Setting a preselected option on a select tag requires you to put a selected attribute on the selected option.

<select style="margin-left:25px;">
<?php
   $options = array(array("null"," "),array("single","Single"),array("in_a_relationship","In a relationship"),array("engaged","Engaged"),array("married","Married"),array("open_rel","In an open relationship"),array("divorced","Divorced"));
   foreach ($option in $options){
       if ($option[0] == $row3['status']){
           $selected = 'selected ';
       }
       else{
           $selected = '';
       }
       echo '<option '.$selected.'value="'.htmlspecialchars($option[0]).'">'.htmlspecialchars($option[1]).'</option>';
   } 
?>
 </select>

Upvotes: 1

Related Questions