How do you find out how many checkboxes have been checked for given Question? For example, you have question called "Hobbies:"with checkbox options of Reading, Biking, Travelling., I want get the count of the hobbies.

Recommended Answers

All 2 Replies

Assuming you are using PHP to process your form, when you create your form
give all your checkboxes the same name BUT add the following suffix "[]" -ex:

<form method="post" ...>
<input type="checkbox" name="hobbies[]" value="Reading"/>
<input type="checkbox" name="hobbies[]" value="Biking"/>
<input type="checkbox" name="hobbies[]" value="Traveling"/>
...
</form>

Then when you submit your form then:

if( isset($_POST['hobbies']) )
  echo 'Total Hobbies: '.count($_POST['hobbies']);
else
  echo 'No Hobbies checked';

<form method="post" ...>
<input type="checkbox" name="hobbies[]" value="Reading"/>
<input type="checkbox" name="hobbies[]" value="Biking"/>
<input type="checkbox" name="hobbies[]" value="Traveling"/>
...
</form>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.