I'm new to PHP and I want this code <option value="" disabled="disabled">-------------</option>
to be disabled when the php code is building my select list of options. How can I fix my php code so it will always set that specific option to disabled?
Here is part of the php code.
echo '<select name="country" id="country" size="20">' . "\n";
foreach($countries as $option) {
if ($option == $state) {
echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
} else {
echo '<option value="'. $option . '">' . $option . '</option>'."\n";
}
}
echo '</select>';
Here is part of the HTML code that is outputed from the php code.
<option value="United States">United States</option>
&开发者_如何学Clt;option value="Australia">Australia</option>
<option value="Canada">Canada</option>
<option value="United Kingdom">United Kingdom</option>
<option value="India">India</option>
<option value="" disabled="disabled">-------------</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
echo '<select name="country" id="country" size="20">' . "\n";
foreach($countries as $option) {
if ($option == $state) {
echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
}
else if($option == "-------------------"){
echo '<option value="" disabled="disabled">----------------</option>';
}
else {
echo '<option value="'. $option . '">' . $option . '</option>'."\n";
}
}
echo '</select>';
Usually, what you do is add these special cases outside the normal flow of the for, unless the database does represent a null option. For example, I could do it like this (using short tags, which might be unavailable):
<select name="country" id="country" size="20">
<option value="" disabled="disabled">----------------</option>
<?php
foreach($countries as $option):
if ($option == $state): ?>
<option value="<?=$option ?>" selected="selected"><?=$option?></option>
<? else: ?>
<option value="<?=$option ?>"><?=$option ?></option>
<? endif;
endforeach; ?>
</select>
I apologize for any syntax errors, I don't have my webserver available right now.
精彩评论