我查看了几个社区论坛,我无法弄清楚如何在验证失败后保留选择选项值。
以下是适用于我的代码,但提交提交按钮后,值会消失。
<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice = mysqli_query($con,"Select * from services") ?>
<?php
while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid'];?>"> <?php echo $line['service'];?> </option>
<?php
}
?>
</select>
以下是我尝试过但对我不起作用的内容:
<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice = mysqli_query($con,"Select * from services") ?>
<?php
while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option>
<?php
}
?>
</select>
答案 0 :(得分:1)
<form action="" method="POST">
<select name="list" id="list">
<option value="item1">item1</option>
<option value="item2">item2</option>
<option value="item3">item3</option>
</select>
<input type="submit" />
</form>
<script type="text/javascript">
document.getElementById('list').value = "<?php echo $_POST['list']?>";
</script>
答案 1 :(得分:-2)
可能是一个小错误,但发现无效&gt;在以下代码中
<option value="<?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option>
试试这个
<option value="<?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'}?> echo $line['serviceid']; <?php echo $line['service'];?>" </option>
希望这会有所帮助