php/ Get an echo message when 5 or more checkboxes are selec

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cee lippens
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2015 11:42 am

php/ Get an echo message when 5 or more checkboxes are selec

Post by cee lippens »

i have a php file that has a questionnaire with symptoms: i want to display the echo statement ' you may have adhd' if 5 or more checkboxes are selected and the echo statement ' you may not have adhd if less then 5 are selected ..heres what i have so far code wise:


Code: Select all

<?php
      echo 'you selected: <br/>';
	    $check = $_POST['check'];
          foreach($check as $key => $value) {
	        echo $value.'<br/>';
	 
	}

 
?>

<form action="forma.php" method="post">

<input type="checkbox" name="check[]" value="A" /> Often fails to
pay close  attention to details or makes careless mistakes in                            schoolwork, work, or other    activities<BR>
<input type="checkbox" name="check[]" value="B" /> Often has    difficulty         maintaining focus on tasks or play activity<BR>
<input type="checkbox" name="check[]" value="C" />Often does not seem to listen when spoken to directly<BR>

<input type="checkbox" name="check[]" value="D" /> Often does not follow through on instructions and fails to finish schoolwork, chores, or other responsibilities (not due to oppositional behavior or failure to understand  instructions)<BR>
<input type="checkbox" name="check[]" value="E" />Often has difficulty organizing tasks and activities<BR>
<input type="checkbox" name="check[]" value="F" />Often avoids, dislikes, or is reluctant to take part in activities that require continuous mental effort,such as schoolwork or homework.<BR>
<input type="checkbox" name="check[]" value="G" />Often loses things needed for tasks or activities, such as toys, assignments, books, or tools<BR>
<input type="checkbox" name="check[]" value="H" />
Is often easily distracted by extraneous stimuli<BR>
<input type="submit" name="formSubmit" value="Submit" />
</form>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php/ Get an echo message when 5 or more checkboxes are s

Post by Celauran »

I suspect count is what you're looking for.
cee lippens
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2015 11:42 am

Re: php/ Get an echo message when 5 or more checkboxes are s

Post by cee lippens »

thank-you
with using count, sorry i am new to php, what would the code look like for if 5 or more checkboxes have been selected
echo ' you may have adhd?'
5 or less
echo ' you may not have adhd'
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php/ Get an echo message when 5 or more checkboxes are s

Post by Celauran »

Code: Select all

if (count($_POST['check']) >= 5) {
    // Some message
} else {
    // Some other message
}
cee lippens
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2015 11:42 am

Re: php/ Get an echo message when 5 or more checkboxes are s

Post by cee lippens »

thank-you. that worked perfectly!
Post Reply