如何显示选中的复选框和未选中的复选框

时间:2019-10-25 11:58:06

标签: php arrays checkbox foreach

我有5个复选框,用户可以从中选择一个或多个选项。然后,选定的选项将在数据库中更新。然后,用户的选择将在另一页上显示/查看。但是我的问题是,在PHP中执行foreach循环时,我想显示更新的选择以及未选择的选择。

这是5个复选框

<input type="checkbox" name="interest[]" value="fishing">Fishing
<input type="checkbox" name="interest[]" value="camping">Camping
<input type="checkbox" name="interest[]" value="hiking">Hiking
<input type="checkbox" name="interest[]" value="swimming">Swimming
<input type="checkbox" name="interest[]" value="running">Running

<br><br>
<input type="submit" name="submit" value="Submit">

在这里更新代码

if (isset($_POST["submit"])) {

 $interestArr = $_POST['interest'];


$interest = new Interest();
$newArr = implode(',', $interestArr);

$interest->updateInterests($id=19, $newArr);

}

这里显示的代码

<?php 

 $interest = new Interest();
 $interests = $interest->showInterests($userid=19)->interests;

 $newArr = explode(',', $interests);

 foreach ($newArr as $data) {

 echo '<input type="checkbox" name="interest[]" value="'.$data .'" checked>'.$data;
}

更新选项像这样存储在数据库的兴趣列下 钓鱼,露营,跑步

foreach循环将其选中的复选框显示为正确的相应标签。

如何显示其他未选中的复选框,以便用户可能要进行更改?

谢谢。

3 个答案:

答案 0 :(得分:0)

假设您有一个选择列表数组,例如:

$choices = ["Fishing", "Camping" , "Hiking","Swimming" , "Running" ]

用户选择他们的选择后

$interests = ["Fishing" , "Running" ];

在您的情况下,最重要的一行是:

$interest = new Interest();
$interests = $interest->showInterests($userid=19)->interests;
$newArr = explode(',', $interests); 
  

在示例中,假设 $ newArr 等于 $ interests

 foreach ($interests as $interest) {
 echo 'you have choose '.$interest.PHP_EOL;
}

结果:

you have choose Fishing
you have choose Running

对于未选中:

$notInterests = array_diff($choices,$interests);

 foreach ($notInterests as $notInterest) {
 echo 'you have not choose '.$notInterest.PHP_EOL;
}

结果:

you have not choose Camping
you have not choose Hiking
you have not choose Swimming

要在一个循环中处理它:

foreach ($choices as $choice) {

  if(in_array($choice,$interests )){
     echo'you have choose '.$choice.PHP_EOL ;
  }else{
      echo 'you have not choose '.$choice.PHP_EOL;
  }

}

希望这对您有所帮助。

答案 1 :(得分:0)

这是一个简单的例子来说明主要思想。该代码旨在在单个脚本中运行。

主要思想是:

  • 使用兴趣的 global 列表来驱动表单。
  • 保留一个单独的 global 列表,其中包含 checked 复选框,您可以将它们进行比较以确定是否应选中该复选框。
  • 提交表单后,填充跟踪已检查项目的列表
  • 呈现表单并将可用复选框的列表与选中的复选框进行比较。如果在两个列表中都找到了该项目,则意味着我们要将该复选框显示为选中状态。

index.php

<?php
// Keep this outside of the if statement so the form has access to it.
$availableInterests = [
    'fishing',
    'camping',
    'hiking',
    'swimming',
    'running',
];

// Keep this outside of the if statement so the form has access to it.
$selectedInterests = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Cast the posted interests to an array in case the user submitted an empty list. 
    // An empty list would be NULL if we didn't cast it. 
    $selectedInterests = (array)$_POST['interest'];

    // foreach $selectedInterests insert into DB...

    // Let this code `fall through` to render the form again.
    // $selectedInterests is now populated and can be used in to the form below to keep the selected checkboxes checked.
}
?>

<form method="post">

    <!-- Using the `global` $availableInterests array to drive our form. -->
    <? foreach ($availableInterests as $interest): ?>

        <!-- Do we want to render the checkbox as checked? -->
        <?php $checked = (in_array($interest, $selectedInterests)) ? ' checked' : ''; ?>

        <input type="checkbox"
               name="interest[]"
               value="<?php echo $interest; ?>"
            <?php echo $checked; ?>>
        <?php echo ucfirst($interest); ?>

    <?php endforeach; ?>

    <br><br>

    <input type="submit" name="submit" value="Submit">

</form>

答案 2 :(得分:0)

为帮助其他可能处于类似情况的人,我想至少根据穆罕默德·雅西·查伯利(Mohammed Yassine CHABLI)的灵感,发布对我来说至少是可行的解决方案。最早发表评论的Vantiya确实帮助我理解了接下来的事情。谢谢大家。

<?php

  $interest = new Interest();
  $interests = $interest->showInterests($userid=19)->interests;
  $choices = $interest->showInterests($userid=19)->choices;

  $selected = explode(',', $interests);
  $choices = explode(',', $choices);

  foreach ($choices as $choice) {

   if(in_array($choice,$selected )){
        echo '<input type="checkbox" name="interest[]" value="'.$choice .'" 
    checked>'.$choice;

    }else{
     echo '<input type="checkbox" name="interest[]" value="'.$choice .'" 
      unchecked>'.$choice;

    } 
}