Jump to content

Recommended Posts

I have a huge FORM with mostly checkboxes. How can I get the number (count) of checkboxes per group. I'm guessing I would have to using something like count($_POST).

So how can I determine the number of checkboxes in the group "TypeN" and then for the group "relationN", etc.

For example:
<input name="Type1" type="checkbox" id="Type1" value="checked" />
<input name="Type2" type="checkbox" id="Type2" value="checked" />
<input name="Type3" type="checkbox" id="Type3" value="checked" />
<input name="Type4" type="checkbox" id="Type4" value="checked" />

<input name="relation1" type="checkbox" id="relation1" value="checked" />
<input name="relation2" type="checkbox" id="relation2" value="checked" />
<input name="relation3" type="checkbox" id="relation3" value="checked" />
<input name="relation4" type="checkbox" id="relation4" value="checked" />
<input name="relation5" type="checkbox" id="relation5" value="checked" />

Link to comment
https://forums.phpfreaks.com/topic/7921-php-checkbox-count/
Share on other sites

[!--quoteo(post=366722:date=Apr 20 2006, 08:39 AM:name=twome)--][div class=\'quotetop\']QUOTE(twome @ Apr 20 2006, 08:39 AM) [snapback]366722[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have a huge FORM with mostly checkboxes. How can I get the number (count) of checkboxes per group. I'm guessing I would have to using something like count($_POST).

So how can I determine the number of checkboxes in the group "TypeN" and then for the group "relationN", etc.

For example:
<input name="Type1" type="checkbox" id="Type1" value="checked" />
<input name="Type2" type="checkbox" id="Type2" value="checked" />
<input name="Type3" type="checkbox" id="Type3" value="checked" />
<input name="Type4" type="checkbox" id="Type4" value="checked" />

<input name="relation1" type="checkbox" id="relation1" value="checked" />
<input name="relation2" type="checkbox" id="relation2" value="checked" />
<input name="relation3" type="checkbox" id="relation3" value="checked" />
<input name="relation4" type="checkbox" id="relation4" value="checked" />
<input name="relation5" type="checkbox" id="relation5" value="checked" />
[/quote]
try[code]<form action="" method="POST">
Type <br />
<input name="Type[]" type="checkbox" id="Type1" value="1" />
<input name="Type[]" type="checkbox" id="Type2" value="2" />
<input name="Type[]" type="checkbox" id="Type3" value="3" />
<input name="Type[]" type="checkbox" id="Type4" value="4" />
<br />relation<br />
<input name="relation[]" type="checkbox" id="relation1" value="1" />
<input name="relation[]" type="checkbox" id="relation2" value="2" />
<input name="relation[]" type="checkbox" id="relation3" value="3" />
<input name="relation[]" type="checkbox" id="relation4" value="4" />
<input name="relation[]" type="checkbox" id="relation5" value="5" />
<br />
<input type="submit" name="submit">
</form>

<?php
if (isset($_POST['submit'])){
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    $t = isset($_POST['Type']) ? count($_POST['Type']) : 0;
    $r = isset($_POST['relation']) ? count($_POST['relation']) : 0;
    echo "User select $t Type ";
        if ($t>0){
        echo "(";
        foreach ($_POST['Type'] as $a) echo "Type$a ";
        echo ") ";
    }
    echo "and $r relation checkbox.";
    if ($r>0){
        echo"(";
        foreach ($_POST['relation'] as $a) echo "relation$a ";
        echo ") ";
    }
    
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-28878
Share on other sites

Actually, I wanted to know not how many boxes the user selected, rather how many boxes there are in that group. (I have a loop that I go through - I need to know how many times to cycle through the loop depending on the number of checkboxes in that group).

For TypeN I would return a count of 4 checkboxes (total for that group TypeN).
And for relationN I would return 5 checkboxes (total in that group for relationN).

For example:
<input name="Type1" type="checkbox" id="Type1" value="checked" />
<input name="Type2" type="checkbox" id="Type2" value="checked" />
<input name="Type3" type="checkbox" id="Type3" value="checked" />
<input name="Type4" type="checkbox" id="Type4" value="checked" />

<input name="relation1" type="checkbox" id="relation1" value="checked" />
<input name="relation2" type="checkbox" id="relation2" value="checked" />
<input name="relation3" type="checkbox" id="relation3" value="checked" />
<input name="relation4" type="checkbox" id="relation4" value="checked" />
<input name="relation5" type="checkbox" id="relation5" value="checked" />

Although, I tried your code - works great!!! Thanks! I'll keep that in mind (what you wrote previously) for good reference in case I need it. (I probably will).


Link to comment
https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-29620
Share on other sites

Ken,
I'm not quite sure what you mean by "on the returned arrays". Could you clarify?

Thanks,
-Coby

[!--quoteo(post=367525:date=Apr 22 2006, 04:53 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 22 2006, 04:53 PM) [snapback]367525[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If you uses sasa's code for your form, all you have to do is use the count() on the returned arrays since only those check boxes that are actually checked are returned.

Ken
[/quote]
Link to comment
https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-30410
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.