1

I have this code,How would you set a default selection in this dropdown menu.

<tr><td class="tdt">

<?php te('Hypervisor');?>:</td> <td title='Add more from Hypervisor menu'>
<select validate='required:true' class='mandatory' name='hyp'>
   <option value=''>Select</option>
  <?php 
    foreach ($hyper as $a) {
      $dbid=$a['id'];
      $atype=$a['typedesc']; $s="";
      if (isset($hyp) && $hyp==$a['id']) $s=" SELECTED ";
      echo "<option $s value='$dbid' title='$dbid'>$atype</option>\n";
    }
    echo "</select>\n";
  ?>

Thanks for your help.

UPDATE:

I have a table connected to this code called hypervisors.in that table theres id field and typedesc field.I'd like to set "ESXi" as the default value which is represented by an ID num 1.

Update 2:

$sql="SELECT id,typedesc FROM hypervisors";
$sth=db_execute($dbh,$sql);
while ($r=$sth->fetch(PDO::FETCH_ASSOC)) 
    $hyper[$r['id']]=$r;

that is SQL query

11
  • This exact way. Side note: when you are generating HTML, always htmlspecialchars any dynamic content. Commented Sep 26, 2013 at 8:48
  • We use selected="selected" jsfiddle.net/wFmBE Commented Sep 26, 2013 at 8:49
  • @Mr.Alien: Not necessary. Commented Sep 26, 2013 at 8:50
  • title attribute is unnecessary Commented Sep 26, 2013 at 8:50
  • @Adam may I guess I got this thing wrong... Commented Sep 26, 2013 at 8:51

2 Answers 2

3

use this:

  foreach ($hyper as $a) {
      $dbid=$a['id'];
      $atype=$a['typedesc'];
      if (isset($hyp) && $hyp==$dbid) {
         echo "<option selected='selected' value='$dbid'>$atype</option>\n";
      } else {
         echo "<option value='$dbid'>$atype</option>\n";
      }

    }
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your reply .Your code did work but it won't select anything as default or do i have to set the default value myself.thanks
@Carter this is because of your condition $hyp==$dbid is not working. check that $hyp has some value or not.
Thanks Broken Heart ღ.My code work perfectly without the default value.Its just it'd be convenient to set default so the user can easily navigate the form without making a whole lot of selections
@Carter ohk, my pleasure to help you. in future you can ask for other problems solution.
Also, adding this logic with the ternary operators lessens the code within one line. :)
1

With selected within the option tag, you set the selected default value of your dropdown http://www.w3schools.com/tags/att_option_selected.asp

if (isset($hyp) && $hyp==$a['id']){
      $s=" SELECTED ";
      echo "<option $s value='$dbid' selected>$atype</option>\n";
}
else {
     echo "<option value='$dbid'>$atype</option>\n";
}

2 Comments

thanks for reply but the code didn't work i'm getting a syntax error. appreciate your help
Did you replace your if (isset($hyp) && $hyp==$a['id']) $s=" SELECTED "; echo "<option $s value='$dbid' title='$dbid'>$atype</option>\n"; with mine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.