我有一个PHP表单,用于编辑MySQL数据库中的记录,它有一个值的下拉列表我想将数据库中保存的选项设置为列表中的第一个因此,如果用户不想更改选项,则数据库不会被默认选项
覆盖任何人都可以建议一个非常简单的解决方案吗?
根据评论,我确实需要帮助才能设置我选择的HTML选择值
<select id="inputRegion" name="inputRegion">
<option name="inputRegion" id="inputRegion" value="Scotland">Scotland</option>
<option name="inputRegion" id="inputRegion" value="North West">North West</option>
<option name="inputRegion" id="inputRegion" value="North East">North East</option>
<option name="inputRegion" id="inputRegion" value="Yorkshire and Humberside">Yorkshire & Humberside</option>
<option name="inputRegion" id="inputRegion" value="East Midlands">East Midlands</option>
<option name="inputRegion" id="inputRegion" value="East Anglia">East Anglia</option>
<option name="inputRegion" id="inputRegion" value="London">London</option>
<option name="inputRegion" id="inputRegion" value="South East">South East</option>
<option name="inputRegion" id="inputRegion" value="South West">South West</option>
<option name="inputRegion" id="inputRegion" value="Wales">Wales</option>
<option name="inputRegion" id="inputRegion" value="West Midlands">West Midlands</option>
<option name="inputRegion" id="inputRegion" value="Northern Ireland">Northern Ireland</option>
</select>
我的php来自查询,然后可以使用;
获取当前记录的区域<?php echo $contact['Region']; ?>
答案 0 :(得分:2)
如果这是一个小的下拉列表,这很快捷
<label>Type: </label>
<?php
if(isset($item->type_id))
$tid = $item->type_id;
else
$tid = 1; // Set this to your preferred default
?>
<select name="type">
<option value="1" <?php if($tid == 1) echo ' selected' ?>>Type 1</option>
<option value="2" <?php if($tid == 2) echo ' selected' ?>>Type 2</option>
<option value="3" <?php if($tid == 3) echo ' selected' ?>>Type 3</option>
</select>
如果是更大的下拉列表,请使用迭代方法编写一个php函数来创建下拉列表
编辑 * 这是正确的,因为它现在默认为$ tid
<?php
if(isset($contact['Region']))
$tid = $contact['Region'];
else
$tid = 'South East'; // Set this to your preferred default
?>
<select id="inputRegion" name="inputRegion">
<option name="inputRegion" id="inputRegion" value="Scotland" <?php if($tid == 'Scotland') echo ' selected' ?>>Scotland</option>
<option name="inputRegion" id="inputRegion" value="North West" <?php if($tid == 'North West') echo ' selected' ?>>North West</option>
<option name="inputRegion" id="inputRegion" value="North East" <?php if($tid == 'North East') echo ' selected' ?>>North East</option>
<option name="inputRegion" id="inputRegion" value="Yorkshire and Humberside" <?php if($tid == 'Yorkshire and Humberside"') echo ' selected' ?>>Yorkshire & Humberside</option>
<option name="inputRegion" id="inputRegion" value="East Midlands" <?php if($tid == 'East Midlands') echo ' selected' ?>>East Midlands</option>
<option name="inputRegion" id="inputRegion" value="East Anglia" <?php if($tid == 'East Anglia') echo ' selected' ?>>East Anglia</option>
<option name="inputRegion" id="inputRegion" value="London" <?php if($tid == 'London') echo ' selected' ?>>London</option>
<option name="inputRegion" id="inputRegion" value="South East" <?php if($tid == 'South East') echo ' selected' ?>>South East</option>
<option name="inputRegion" id="inputRegion" value="South West" <?php if($tid == 'South West') echo ' selected' ?>>South West</option>
<option name="inputRegion" id="inputRegion" value="Wales" <?php if($tid == 'Wales') echo ' selected' ?>>Wales</option>
<option name="inputRegion" id="inputRegion" value="West Midlands" <?php if($tid == 'West Midlands') echo ' selected' ?>>West Midlands</option>
<option name="inputRegion" id="inputRegion" value="Northern Ireland" <?php if($tid == 'Northern Ireland') echo ' selected' ?>>Northern Ireland</option>
</select>
答案 1 :(得分:0)
您可以使用
while($row = mysql_fetch_assoc($result)){
$selected = "";
if($row['id'] == $curValue){
$selected = "selected=\"selected\"";
}
echo "<option value=\"{$row['id']}\" $selected>{$row['name']}</option>
}
在您更新问题之后,MattP的答案似乎更适合您的情况。