就我而言,我只得到最后一个复选框值,而不显示所有选中的复选框值。 一共有三页 在menu.php中,我从用户
获得了多个复选框值<form method="POST" action="internal.php">
<input type="checkbox" name="sides[]" value="Cheese" ><label>Cheese </label>
<input type="checkbox" name="sides[]" value="Broccoli"><label>Broccoli </label>
<input type="checkbox" name="sides[]" value="Carrots"><label>Carrots</label>
</form>
internal.php(在这里我在会话中存储了多个复选框值。)
<?php
session_start();
if(!empty($_POST['sides'])) {
foreach($_POST['sides'] as $check) {
$_SESSION['sides']=$check;
echo $_SESSION['sides']; // if i don't use header location then here i get all checkbox value
echo "<br>";
}
}
header('location: show.php');
?>
最后一页,即show.php,我试图显示多个复选框值
<?php if(isset($_SESSION['sides'])){ ?>
<div class="col-lg-4 col-md-4 col-sm-6">
<p><?php echo $_SESSION['sides']; ?></p> // here i get only last checked checkbox value
</div>
<?php unset($_SESSION['sides']); } ?>
答案 0 :(得分:0)
您实际上并没有在会话中保存所有复选框值,而只是将最后一个保存为该行
$_SESSION['sides']=$check;
每次循环都覆盖$_SESSION['sides']
因此将值连接起来
<?php
session_start();
if(!empty($_POST['sides'])) {
foreach($_POST['sides'] as $check) {
$_SESSION['sides'] .= $check;
}
}
header('location: show.php');
?>
您可能还希望在其中添加分隔符
<?php
session_start();
if(!empty($_POST['sides'])) {
foreach($_POST['sides'] as $check) {
$_SESSION['sides'] .= $check . ',';
}
}
rtrim($_SESSION['sides'], ','); // remove last comma seperator
header('location: show.php');
?>
其他说明:请记住,PHP仅会发送“已检查”复选框值。