Reputation: 1037
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
Upvotes: 1
Views: 5396
Reputation: 165
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";
}
Upvotes: 1
Reputation: 15603
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";
}
}
Upvotes: 3