<?php
$options = array('volvo'=>'Volvo', 'saab' =>'Saab', 'audi' => 'Audi' );
echo "<select name='sss'>\n";
foreach ($options as $k=>$v) echo "<option value='$v' >$k</option>\n";
echo "</select>\n";
?>
Question:
how to make 'audi' as the default value instead of 'volvo'? I know we can set 'selected' in html, but how could i do it in this php script?
You could detect if the default value is the one you want and insert the HTML you already identified:
$defaultVal = 'Audi';
foreach ($options as $k=>$v) {
$selected = ($v == $defaultVal) ? " selected='selected'" : "";
echo "<option value='$v'$selected>$k</option>\n";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With