>我正在尝试从数据库中获取数据,所有值都反映了良好,但未选择下拉列表的保存值。
<div class="col-md-4">
<select name="OfficeName" id="OfficeName" class="form-control" onchange="getText(this)" required>
<?php
while($data = dbFetchAssoc($result)){
?>
<option value=''><?php echo $data['off_name']; ?></option>
<?php
}//while
?>
</select>
</div>
这是一个常见问题。
假设您有一个 select 语句,其中填充了数据库中的键值对:
<select name="sel">
<?php foreach ($options as $value => $label) : ?>
<option value="<?= $value ?>"><?= htmlentities($label) ?></option>
<?php endforeach ?>
提交表单后,您将获得 _GET 美元或 _POST 美元(或 _REQUEST 美元)的 sel 值。
<?php
$selected = $_POST['sel'];
?>
要选择相应的选项,请更新上面的代码,如下所示:
<select name="sel">
<?php foreach ($options as $value => $label) : ?>
<option <?php if ($selected == $value) : ?>selected<?php endif ?> value="<?= $value ?>">
<?= htmlentities($label) ?>
</option>
<?php endforeach ?>