Results 1 to 10 of 10

Thread: Count checkbox

  1. #1
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Count checkbox

    Is there a way to count the number of check boxes that are checked, while they are still putting information into a database?

    I have an order form, with options on it, but each option needs to increas the total price. I need to record the amout of options, and post the options in the database. I can post the array, but don't have a clue on how to do the count.

    Thanks...

  2. #2
    Join Date
    Nov 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    that's a JavaScript Question

    and the answer is YES

    with JS:

    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function CountCheck(){
       var chkCnt = 0;
       var Frm = document.forms[0]; // or name where the first form is 0
       var dLen = Frm.length - 1;
       for (i=0;i<=dLen;i++){
         if (Frm.type == 'checkbox') chkCnt++;
       }
    } // end function
    //-->
    </script>

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    With PHP, you could check that too.

    I can't remember the value sent via POST/GET to PHP, but I think it's 1. Maybe TRUE.

    Anyway, you could search through the entire post/get array for any "1" values but that would include ANY, not just the checkboxes.

    Really, you would need to just check each individually and for each add 1 to $count or something.

    And, if you need this in realtime without submitting the form, then you would need to use Javascript.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    I can't remember the value sent via POST/GET to PHP, but I think it's 1. Maybe TRUE.
    The value of the control. All form controls send the current value (the value property via the DOM), paired with the control name.

    If the control names are the same for all related checkboxes - and they often should be - then those names should end with a pair of square brackets: []. PHP will then construct an array of these values, and the length of that array will be the number of checked checkboxes.

    For example, with a group of checkboxes (or any control, for that matter) named "options[]" (name="options[]") then there would be an array named "options" in the $_GET or $_POST superglobals (depending on the method used to submit the form, obviously). If no checkboxes were checked, then no element would exist. That is,

    PHP Code:
    isset($_POST['options']) 
    would return false.

    Mike

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    But how would you check how many? The question isn't checking if none exist, though that is interesting.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Acey99 View Post
    that's a JavaScript Question

    and the answer is YES

    with JS:

    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function CountCheck(){
       var chkCnt = 0;
       var Frm = document.forms[0]; // or name where the first form is 0
       var dLen = Frm.length - 1;
       for (i=0;i<=dLen;i++){
         if (Frm.type == 'checkbox') chkCnt++;
       }
    } // end function
    //-->
    </script>

    Cool...thanks.

    I really don't know JS at all, but this will work if I know the variable I am looking for. Is $i my ouput?

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    But how would you check how many?
    I already answered that: the length of the array (if it exists) represents the number of checked checkboxes.

    PHP Code:
    $checkboxCount = isset($_POST['options']) ? count($_POST['options']) : 0
    If the checkboxes don't have a similar array-like control name[1], things become harder. One solution is to list the names in an array, then loop through them, checking if an element exists:

    PHP Code:
    $checkboxes = array('foo''bar' /* ... */);
    $checkboxCount 0;

    foreach (
    $checkboxes as $name) {
        if (isset(
    $_POST[$name])) ++$checkboxCount;


    Quote Originally Posted by rocg23 View Post
    I really don't know JS at all, but this will work if I know the variable I am looking for. Is $i my ouput?
    If you're doing this server-side - I assume you are due to database interaction - then client-side scripting is useless at this stage.

    Mike


    [1] As well as naming form controls with a trailing, empty bracket pair, those brackets can contain a value. The array will still be created as before, but rather than having numeric indices, results can be accessed by name. Consider:

    HTML Code:
    <form action="..." method="post">
        <fieldset>
            <legend>Personal details</legend>
            <label>First name:
                <input name="personal[first-name]" type="text" value=""></label>
            <label>Last name:
                <input name="personal[last-name]" type="text" value=""></label>
        </fieldset>
    </form>
    When submitted, the $_POST superglobal will contain an element named 'personal'. This will be an array with two elements, 'first-name' and 'last-name'.

  8. #8
    Join Date
    Nov 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    in regards to rocg23 on the JS output.

    chkCnt is the number of checkboxes.

    yes PHP can do it as well, but sometimes why?

    USING $_POST[] or $_GET (Depends on what <form Method="" is

    count($_POST["checkBoxName]);
    or
    count($_GET["checkBoxName]);

    also need to at least have one checkbox checked in this case if using server side.

  9. #9
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Acey99 View Post
    chkCnt is the number of checkboxes.
    A value that is inaccessible because it is local and never returned.

    yes PHP can do it as well, but sometimes why?
    In this instance, because the information is necessary server-side (assuming I understand the OP correctly), and there is no benefit to calculating it client-side.

    Sites should not rely on client-side scripting. A site should work with only server-side support in the first instance. Client-side code can then be added where it helps the user or makes actions more efficient. Even if something is engineered such that the client does all of the work, if that feature is important (and here is surely is), then a server-side fall back is required.

    count($_POST["checkBoxName]);
    or
    count($_GET["checkBoxName]);

    also need to at least have one checkbox checked in this case if using server side.
    No, you don't. I already posted how to do this.

    Mike

  10. #10
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    [QUOTE=mwinter;61706]I already answered that: the length of the array (if it exists) represents the number of checked checkboxes.

    PHP Code:
    $checkboxCount = isset($_POST['options']) ? count($_POST['options']) : 0

    Thanks mwinter...This did exactly what I needed it to do...returned the number checked, and I verified that it came back 0 with nothing checked.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •