How to dynamically create checkboxes in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarega
    New Member
    • Oct 2008
    • 82

    How to dynamically create checkboxes in javascript

    Hi,
    There is an array whose length varies dynamically on the users input, I have to provide checkboxes for this array along with the value of the array and based on the checkboxes selected by the user the value of that checkbox has to be retrived back at the php code for further processing.
    NOTE:The value of the array x changes dynamically, here it is 3, it can even be more or less than that.
    Code:
    <?
    $x=array('a','b','c');
    for($i=0;$i<count($x);$i++) {
         echo "<input type='checkbox' id='chk[$i]' onclick=\"list_array($x[$i]\" />$x[$i]<br>";
    }
    Javascript code:
    Code:
    function list_array(x) {
    Here I have to collect all the values of array for which the checkbox is clicked and later return all the elements of the array back to the php function.
    }
    Can somebody please help me with this??

    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    wouldn't it be easier to put a reference value to the checkbox and read in PHP from that the required input data?
    Code:
    // HTML
    <input type="checkbox" name="selection[]" value="example"/> // note the value
    <input type="text" name="example" ... /> // note the name
    
    // PHP
    foreach ($_POST['selection'] as $selected)
    {
        do_something_with($_POST[$selected]);
    }

    Comment

    • sarega
      New Member
      • Oct 2008
      • 82

      #3
      are you telling that the javascript is not required at all?? then how to accept the users input of checking a checkbox?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        yup.
        if a checkbox is checked, its value is submitted in the form data (if the checkboxes have a name attribute)

        PS: I just finished such a script 3 days ago

        Comment

        • sarega
          New Member
          • Oct 2008
          • 82

          #5
          can you please give me example of it?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by sarega
            can you please give me example of it?
            see post #2.
            Code:
            // HTML
            // the square brackets force an array holding all checked values,
            // that is, all names of the input fields you want to work with
            <input type="checkbox" name="selection[]" value="example"/> 
            
            // the element referenced by the checkbox' value
            <input type="text" name="example" ... />
            
            // PHP
            // always test, that your working variable really exists
            if (!isset($_POST['selection'])) return false;
            
            // you can use a for or while loop too, depending on your requirement
            // loop through all checked checkboxes
            foreach ($_POST['selection'] as $selected)
            {
                // $selected is the name of the <input> field,
                // $_POST[$selected] is the value of the <input> field
                // whose checkbox was checked
                do_something_with($_POST[$selected]); // this is the placeholder for your code
            }

            Comment

            • sarega
              New Member
              • Oct 2008
              • 82

              #7
              If I try this am getting the value printed in the php page

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                is that good or bad?

                Comment

                • sarega
                  New Member
                  • Oct 2008
                  • 82

                  #9
                  actually I do not want the value to be printed....it should print
                  for eg.
                  chkbox1 abc
                  chkbox2 bcd
                  chkbox3 cde
                  it should display abc,bcd and cde

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by sarega
                    actually I do not want the value to be printed....
                    then do not print out the value..... that is, change the PHP code so that it prints what you want. or am I getting something wrong?

                    Comment

                    • sarega
                      New Member
                      • Oct 2008
                      • 82

                      #11
                      Code:
                      <?
                       $x=array('a','b','c');
                       for($i=0;$i<count($x);$i++) {
                            echo "<input type='checkbox' id='chk[$i]' onclick=\"list_array($x[$i]\" />$x[$i]<br>";
                       }
                      Last edited by Dormilich; Jan 19 '09, 02:19 PM. Reason: fixed [code] tags

                      Comment

                      • sarega
                        New Member
                        • Oct 2008
                        • 82

                        #12
                        in the above code if the checkbox1 is clicked then 'a' has to be passed to the php file. Similarly for other checkboxes

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          put the "a" in the checkbox's value attribute and give the checkbox a name attribute.

                          Comment

                          • sarega
                            New Member
                            • Oct 2008
                            • 82

                            #14
                            but the number of checkboxes changes dynamically in that case how do i assign that value to the checkbox

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              how do you determine, how many checkboxes you have/need?

                              Comment

                              Working...