我遇到此代码无法保留提交时dropdown
的值。
$uCars=array("BMW","Honda","XUV","Safari","Mercedes","Volvo");
.....
if ($_POST['uCars'])
$uSelectCar = $_POST['uCars'];
....
<select name="uCar">
<?php
foreach($uCars as $uCar) { ?>
<option value="<?php echo $uCar?>" name="uCars[]" <?php echo if($uCar=$uSelectCar) echo "selected='selected'?> ><?php echo $uCar ?></option>
<?php
} ?>
</select>
答案 0 :(得分:0)
您的选项值构建器错误,您有几个语法错误,在if语句后面有一个未闭合的引用,if语句没有封装,if语句之前有一个echo,而你的比较不正确它应该包含两个等号的两个等号应该是“相同的”:
<?php echo if($uCar=$uSelectCar) echo "selected='selected'?>
实际应该是
<?php if($uCar == $uSelectCar) { echo "selected='selected'" } ?>
完成的代码在
下面<select name="uCars">
<?php
foreach($uCars as $uCar) { ?>
<option value="<?php echo $uCar; ?>" <?php if($uCar == $uSelectCar) { echo "selected='selected'"; } ?>><?php echo $uCar; ?></option>
<?php
} ?>
</select>