Reputation: 1301
I need to have php validation that says at least 2 checkboxes should be checked... I have been trying different things but unable to get the right results.
<?php
if(!empty($_POST['opt'])) {
foreach($_POST['opt'] as $check) {
echo $check;
}
$checkboxes = count($check);
echo '$checkboxes';
}
?>
<form action="index.php" method="post">
<input type="checkbox" name="opt[]" value="option1" />option1<br />
<input type="checkbox" name="opt[]" value="option2" />option2<br />
<input type="checkbox" name="opt[]" value="option3" />option3<br />
<br /><br />
<input type="submit" name="formSubmit" value="Send" />
</form>
Upvotes: 0
Views: 749
Reputation: 9295
You can try this:
<?php
if( is_array($_POST['opt']) )
{
foreach($_POST['opt'] as $check)
echo $check;
if(count($_POST['opt']) >= 2)
{
echo 'Valid';
}
else
{
echo 'Not valid';
}
}
else
echo 'Nothing checked';
?>
Upvotes: 1
Reputation: 808
My example uses the opposite logic to the first person.
count_check = count($_POST['opt'])
if(count_check < 2)
{
//stop submission
}
else
{
//Proceed
}
Upvotes: 1