提交或刷新后在下拉列表中保留值


<?php
$sql=mysqli_query($mysqli,"select * from calls where eid='$aid'")or die(mysqli_error($mysqli));
while($row2=mysqli_fetch_assoc($sql))
{
?><select name='city'>
<option value="">Select City</option>
<?php   
$c=explode(',', $row2['city']);
foreach ($c as $c1) 
{
?>
<option value='$c1'  <?php if(isset($_POST['search']) || isset($_POST['next'])|| isset($_POST['submit']))
{
if(($c==$c1))
{ echo 'Selected';} 
}?>><?php echo $c1;?></option>"
<?php }
echo"</select>";
}
?>

calls表city存储为逗号分隔的数组。我已经成功地将其放入下拉框中。现在,我希望每当我提交或刷新页面时,它都会保存选定的下拉值。

您应该检查isset($_POST["city"]) && $_POST["city"] == $city

foreach ($cities as $city) 
{
$selection = (isset($_POST["city"]) &&  $_POST["city"] == $city  ? 'selected':''  );
echo ' <option value="'.$city.'" '.$selection.' >'.$city.'</option>';

}


您只需要将$c1的值与表单提交返回的上一个值进行比较,然后检查此执行是否是发布表单的结果。

<?php
$sql = "select * from calls where eid=?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $aid)
$stmt->execute();
$result = $stmt->get_result();
while($row2 = $result->fetch_assoc()) {
?>
<select name='city'>
<option value="">Select City</option>
<?php   
$c=explode(',', $row2['city']);
foreach ($c as $c1) {
$sel = '';   // init value 
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && $c1 == $_POST['city'] ) {
$sel = "selected='selected'";
}
echo "<option $sel value='$c1' >$c1</option>";
}
echo "</select>";
}
?>

相关内容

  • 没有找到相关文章

最新更新