如果我的php值等于选项值,我想选择该选项值。我该怎么做?
假设我有这组选项值:
<select id="updateBirthmonth" class="birth-date">
<?php $selected-value = 'May'; ?>
<option>Month:</option>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
答案 0 :(得分:1)
Hey @Brained Washed option do have values.
$updateBirthmonth = $_REQUEST['updateBirthmonth'];
<select id="updateBirthmonth" class="birth-date">
<option value="">Select Month</option>
<option value="1" <?php if($updateBirthmonth=="1") { echo 'selected="selected"'; } else { echo ''; } ?>>January</option>
<option value="2" <?php if($updateBirthmonth=="2") { echo 'selected="selected"'; } else { echo ''; } ?>>February</option>
<option value="3" <?php if($updateBirthmonth=="3") { echo 'selected="selected"'; } else { echo ''; } ?>>March</option>
<option value="4" <?php if($updateBirthmonth=="4") { echo 'selected="selected"'; } else { echo ''; } ?>>April</option>
<option value="5" <?php if($updateBirthmonth=="5") { echo 'selected="selected"'; } else { echo ''; } ?>>May</option>
<option value="6" <?php if($updateBirthmonth=="6") { echo 'selected="selected"'; } else { echo ''; } ?>>June</option>
<option value="7" <?php if($updateBirthmonth=="7") { echo 'selected="selected"'; } else { echo ''; } ?>>July</option>
<option value="8" <?php if($updateBirthmonth=="8") { echo 'selected="selected"'; } else { echo ''; } ?>>August</option>
<option value="9" <?php if($updateBirthmonth=="9") { echo 'selected="selected"'; } else { echo ''; } ?>>September</option>
<option value="10" <?php if($updateBirthmonth=="10") { echo 'selected="selected"'; } else { echo ''; } ?>>October</option>
<option value="11" <?php if($updateBirthmonth=="11") { echo 'selected="selected"'; } else { echo ''; } ?>>November</option>
<option value="12" <?php if($updateBirthmonth=="12") { echo 'selected="selected"'; } else { echo ''; } ?>>December</option>
</select>
I hope this would help you. :)