我有HTML表单,其中包含如下所示的选择选项
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<?php if ($row[occupation] == 0){
}?>
<option value="0">Parent</option>
<option value="1">Teacher</option>
</select>
</div>
我想要
如果($ row [occupation] == 0)否则需要选择称为“父项”的选项 显示老师已选中
。我是PHP新手,不知道该怎么做。让我知道是否有人可以帮助我。 谢谢
答案 0 :(得分:2)
您必须基于selected='selected'
的值来回显$row['occupation']
,如下所示:
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<option value="0"<?php if($row['occupation'] == 0){ echo " selected='selected'"; } ?>>Parent</option>
<option value="1"<?php if($row['occupation'] == 1){ echo " selected='selected'"; } ?>>Teacher</option>
</select>
</div>
答案 1 :(得分:2)
您可以使用这个。
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name="occupation" required>
<option value="0" <?php if ($row['occupation'] == 0) { echo 'selected'; } ?>>Parent</option>
<option value="1" <?php if ($row['occupation'] == 1) { echo 'selected'; } ?>>Teacher</option>
</select>
</div>