/    Sign up×
Community /Pin to ProfileBookmark

Counting form checkboxes

I have a form containing checkboxes. Once the form is submnitted I know that count($_POST[‘arrayname’]) return the numbers of checked checkboxes. Can PHP count the number of checkboxes in a form regardless of checked or not? Or do I need Javascript for that?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 25.2011 — Unfortunately, the HTML specification has it that only checked checkboxes get sent in the form data, so PHP will not know about any unchecked ones. You could certainly do something with JavaScript, with the usual caveat that JavaScript may not be enabled by all users, plus a malicious user can easily bypass it. You could consider as an alternative to add a hidden field for each checkbox with a different name but the same naming convention (e.g. using the "[]" array naming convention) to get your total via that name.
<i>
</i>&lt;label&gt;&lt;input type="checkbox" name="check[]" value="1" /&gt;Foo&lt;/label&gt;
&lt;input type="hidden" name="all_check[]" value="0" /&gt;
&lt;label&gt;&lt;input type="checkbox" name="check[]" value="2" /&gt;Bar&lt;/label&gt;
&lt;input type="hidden" name="all_check[]" value="0" /&gt;
Copy linkTweet thisAlerts:
@RPJDauthorFeb 25.2011 — Thanks for the suggestion NogDog.
×

Success!

Help @RPJD spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...