1

I have a row of checkboxes and my aim is to have a script which checks if at least one out of the three is checked.

Currently, I can check if an individual checkbox is checked by adding a name to a checkbox (e.g name="checkbox") and using if(empty($_POST['checkbox'])).

Here's my code currently and the script I have tried to check if at least one checkbox is checked:

<form method="post">
<ul>
<li><input type="checkbox">Example</li>
<li><input type="checkbox">Example</li>
<li><input type="checkbox">Example</li>
</ul>
<input type="submit" name="submit">
</form>

<?php 
if(isset($_POST['submit'])) {
$container = $_POST['list'];

if(empty($container)) {
  // checkbox checked
}else{
  // checkbox not checked
}
}
?>
The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • this is failing you here `$_POST['list']`.You can't use those with an `
      ` not in pure PHP anyway. You need to name the inputs with different values.
    – Funk Forty Niner May 04 '16 at 16:21
  • @PedroLobito The same name to each checkbox? – The Codesee May 04 '16 at 16:21
  • @Fred-ii- Thanks for pointing them two issues out. I moved the `name="list` from the `ul` element to `form`. – The Codesee May 04 '16 at 16:23
  • I think this already has the answer you want ... http://stackoverflow.com/questions/4554758/how-to-read-if-a-checkbox-is-checked-in-php – Rui Costa May 04 '16 at 16:23
  • You're welcome. What you need to do here is use a `foreach` and a `count if`, and check it on an `isset()` and treat them as arrays if they're all going to hold the same name attribute. – Funk Forty Niner May 04 '16 at 16:23
  • now you edited `name="list"` - Forms don't use name attributes, not for what you want to do here – Funk Forty Niner May 04 '16 at 16:25
  • @Fred-ii- My mistake again, apologies. – The Codesee May 04 '16 at 16:26
  • @RuiCosta I had a look at that thread and a few others, however they all refer to checking if an individual checkbox is checked, not checking if at least one checkbox out of a group is checked. – The Codesee May 04 '16 at 16:31
  • @TheCodesee I posted something below and could work for what you want to do. If it doesn't, I'll just delete it. There are 2 in there. – Funk Forty Niner May 04 '16 at 16:42

2 Answers2

1

This is too long for a comment at this point and have already outlined what you need to do in comments.

N.B.: I pulled this from a "now deleted" question as I was going to make a reference to it.

Note that if you're wanting to use the same name attribute for each checkbox, that you need to treat them as arrays using [] for all of them.

I.e.: name="the_name[]"

Base yourself on the following:

<?php

    if(isset($_POST['test'])){
        $a_counts = array();
        foreach($_POST['test'] as $key => $val){
            if(!isset($a_counts[$val])){
                $a_counts[$val] = 1;
            }else{
                $a_counts[$val]++;
            }
            echo $key." => ".$val."<br>";
        }
        print_r($a_counts);
    }
?>
<form action="" method="POST">
    <input type="checkbox" name="test[]" value="red">
    <input type="checkbox" name="test[]" value="green">
    <input type="checkbox" name="test[]" value="yellow">
    <input type="checkbox" name="test[]" value="blue">
    <input type="submit" value="ok">
</form>

Or something like this which would be the better solution:

<form method="post">

    <label class="heading">Select from the following:</label>
    <input type="checkbox" name="check_list[]" value="Value 1"><label>Value 1</label>
    <input type="checkbox" name="check_list[]" value="Value 2"><label>Value 2</label>
    <input type="checkbox" name="check_list[]" value="Value 3"><label>Value 3</label>
    <input type="checkbox" name="check_list[]" value="Value 4"><label>Value 4</label>
    <input type="checkbox" name="check_list[]" value="Value 5"><label>Value 5</label>
    <input type="submit" name="submit" Value="Submit"/>

</form>

<?php 

      if (isset($_POST['submit'])) {
          if (!empty($_POST['check_list'])) {
              // Counting number of checked checkboxes.
              $checked_count = count($_POST['check_list']);
              echo "You have selected following ".$checked_count." option(s): <br/>";
              // Loop to store and display values of individual checked checkbox.
              foreach ($_POST['check_list'] as $selected) {
                  echo "<p>".$selected ."</p>";
              }
          }
          else {
              echo "<b>Please Select at least One Option.</b>";
          }
      }
    ?>

<?php 

if (!empty($_POST['check_list'])) {
          // Counting number of checked checkboxes.
          $checked_count = count($_POST['check_list']);
          echo "You have selected following ".$checked_count." option(s): <br/>";
           // Loop to store and display values of individual checked checkbox.
           foreach ($_POST['check_list'] as $selected) {
               echo "<p>".$selected ."</p>";
           }
       }
       else {
           echo "<b>Please Select at least One Option.</b>";
       }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks for the excellent solution to the problem, I wasn't aware that `input` elements could be given the same `name`... I'm more of a PHP guy – The Codesee May 04 '16 at 16:57
  • @TheCodesee You're welcome, glad to have helped, *cheers*. And yes they can just as long (and if given the same name) as they are treated as arrays with the brackets `[]`. – Funk Forty Niner May 04 '16 at 17:00
0

First you have to make your sure you post a array of values from the checkbox

<form method="post" action="file.php">
<ul>
<li><input name="checkbox[]" type="checkbox">Example</li>
<li><input name="checkbox[]" type="checkbox">Example</li>
<li><input name="checkbox[]" type="checkbox">Example</li>
</ul>
<input type="submit" name="submit">
</form>

Next you post to your php page in this case file.php

<?php
 if(isset($_POST['checkbox'])){
  //Atleast one is checked
 }
?>

Or you could loop the checkbox's

<?php 
  $checkboxArray = $_POST['checkbox'];
  foreach($checkboxArray as $checkbox){
   if(isset($checkbox)){
    $isOneSelected = true;
   }
  }
?>