表格 select 选项 selected PHP
Form select option selected PHP
我试图找到如何制作包含当前日期的选项标签 selected 的方法。
例如,如果我的字符串 $currentDay=03;
被 PHP date('d');
调用,我希望我的 select 像这样:
<select>
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
...
...
</select>
<?php $day=date('d');?>
<select>
<option value="01" <?=($day=='01')?'selected':'';?> >01</option>
<option value="02" <?=($day=='02')?'selected':'';?> >02</option>
<option value="03" <?=($day=='03')?'selected':'';?> >03</option>
<option value="04" <?=($day=='04')?'selected':'';?> >04</option>
...
...
</select>
您可以使用以下代码:
$day = date('d');
$selected = ' selected="selected" ';
<select>
<option value="01" <?php if($day=='01') echo $selected; ?>>01</option>
<option value="02" <?php if($day=='02') echo $selected; ?>>02</option>
<option value="03" <?php if($day=='03') echo $selected; ?>>03</option>
<option value="04" <?php if($day=='04') echo $selected; ?>>04</option>
...
...
</select>
<select>
<? foreach ($options as $option) : ?>
<option value="<?= $option ?>" <? if ($selected_option==$option) echo 'selected' ?>>
<?= $option ?>
</option>
<? endforeach ?>
</select>
我试图找到如何制作包含当前日期的选项标签 selected 的方法。
例如,如果我的字符串 $currentDay=03;
被 PHP date('d');
调用,我希望我的 select 像这样:
<select>
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
...
...
</select>
<?php $day=date('d');?>
<select>
<option value="01" <?=($day=='01')?'selected':'';?> >01</option>
<option value="02" <?=($day=='02')?'selected':'';?> >02</option>
<option value="03" <?=($day=='03')?'selected':'';?> >03</option>
<option value="04" <?=($day=='04')?'selected':'';?> >04</option>
...
...
</select>
您可以使用以下代码:
$day = date('d');
$selected = ' selected="selected" ';
<select>
<option value="01" <?php if($day=='01') echo $selected; ?>>01</option>
<option value="02" <?php if($day=='02') echo $selected; ?>>02</option>
<option value="03" <?php if($day=='03') echo $selected; ?>>03</option>
<option value="04" <?php if($day=='04') echo $selected; ?>>04</option>
...
...
</select>
<select>
<? foreach ($options as $option) : ?>
<option value="<?= $option ?>" <? if ($selected_option==$option) echo 'selected' ?>>
<?= $option ?>
</option>
<? endforeach ?>
</select>