mercredi 4 mai 2016

Verifying at least one checkbox is checked php

I have a row of checkboxes and my aim is to have a script which checks if at least one 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" name="list">
<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
}
}
?>




Aucun commentaire:

Enregistrer un commentaire