Select All Checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raamay
    New Member
    • Feb 2007
    • 107

    Select All Checkbox

    I googled an found out only javascript doing the selecting of all checkbox in a form. Does that mean we there is no other ways to do it?

    If only javascript can perform this operation, then how would it work to those who have disbled javascript. I avoid javascript most of the time, it is therefore i am looking for an alternative to perform the above cited operation. Please advise me!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by raamay
    I googled an found out only javascript doing the selecting of all checkbox in a form.

    ... therefore i am looking for an alternative to perform the above cited operation.
    the description of your operation is a bit vague, maybe I can tell more with some additional details.

    Comment

    • raamay
      New Member
      • Feb 2007
      • 107

      #3
      I have a page displaying some records and each record accompanied with checkbox. So, if i want to delete certain records i can check those records using the checkbox and perform delete operation in one go using a button. In this similar fashion, i also want a control by which i can check all checkbox at one go and delete if necessary.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        since PHP is only executed on the server, user interactions are handled by Javascript.

        To answer your question, there is no other way (as far as I see it).

        Comment

        • secmazec
          New Member
          • Mar 2009
          • 34

          #5
          It would be much easier for all if you either describe your problem better or paste some code.

          First name checkboxes logically from 1...N, so you can go trough all of them easily, something like this, and store how many you have in PHP ($cb_sum in my example):
          Code:
          <input type="checkbox" name="option_1" value="Milk"> Milk<br>
          <input type="checkbox" name="option_2" value="Milk"> Milk<br>
          <input type="checkbox" name="option_3" value="Milk"> Milk<br>
          After the code, you need to store it in javascript, like:
          Code:
          <script>
          var cb_sum = <? echo $cb_sum; ?>
          </script>
          And second part is really easy, you go from 1 to cb_sum in your javascript.

          There is another way, if you need to have something like ID instead of 1..N in checkbox names, but that's more complicated.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            You could use PHP to check all the boxes, if you are using PHP to generate the boxes and you don't mind a page refresh.

            However, the amount of people not using javascript these days is incredibly small, and if they're not using it, it's their problem and not yours.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by Markus
              ... and if they're not using it, it's their problem and not yours.
              nevertheless, you may leave a note saying your page requires/operates best with JavaScript enabled.

              Comment

              • secmazec
                New Member
                • Mar 2009
                • 34

                #8
                actually if you only need it in PHP, that's very easy:

                Code:
                $selected = array ('a', 'b', 'c'); // list of selected ids
                
                echo "<input type=\"checkbox\" ";
                
                if($id==$selected)
                echo "checked "; // the magic :p
                
                echo "value=\"$val\" name=\"$id\" id=\"$id\" />";
                N'joy

                PS: JavaScript is client based, so it's function depends on their PC and Browser, not server. Hope I've helped ;)

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by secmazec
                  actually if you only need it in PHP, that's very easy:

                  Code:
                  $selected = array ('a', 'b', 'c'); // list of selected ids
                  
                  echo "<input type=\"checkbox\" ";
                  
                  if($id==$selected)
                  echo "checked "; // the magic :p
                  
                  echo "value=\"$val\" name=\"$id\" id=\"$id\" />";
                  N'joy

                  PS: JavaScript is client based, so it's function depends on their PC and Browser, not server. Hope I've helped ;)
                  Of course that would have to be in a loop, otherwise you're comparing $id against an array. ;) Alternatively, you could use in_array().

                  Comment

                  Working...