Connecting Tech Pros Worldwide Forums | Help | Site Map

How to dynamically create checkboxes in javascript

Member
 
Join Date: Oct 2008
Posts: 82
#1: Jan 16 '09
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.
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $x=array('a','b','c');
  3. for($i=0;$i<count($x);$i++) {
  4.      echo "<input type='checkbox' id='chk[$i]' onclick=\"list_array($x[$i]\" />$x[$i]<br>";
  5. }
  6.  
Javascript code:
Expand|Select|Wrap|Line Numbers
  1. function list_array(x) {
  2. 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.
  3. }
  4.  
Can somebody please help me with this??

Thanks

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#2: Jan 16 '09

re: How to dynamically create checkboxes in javascript


wouldn't it be easier to put a reference value to the checkbox and read in PHP from that the required input data?
Expand|Select|Wrap|Line Numbers
  1. // HTML
  2. <input type="checkbox" name="selection[]" value="example"/> // note the value
  3. <input type="text" name="example" ... /> // note the name
  4.  
  5. // PHP
  6. foreach ($_POST['selection'] as $selected)
  7. {
  8.     do_something_with($_POST[$selected]);
  9. }
Member
 
Join Date: Oct 2008
Posts: 82
#3: Jan 16 '09

re: How to dynamically create checkboxes in javascript


are you telling that the javascript is not required at all?? then how to accept the users input of checking a checkbox?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#4: Jan 16 '09

re: How to dynamically create checkboxes in javascript


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
Member
 
Join Date: Oct 2008
Posts: 82
#5: Jan 19 '09

re: How to dynamically create checkboxes in javascript


can you please give me example of it?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#6: Jan 19 '09

re: How to dynamically create checkboxes in javascript


Quote:

Originally Posted by sarega View Post

can you please give me example of it?

see post #2.
Expand|Select|Wrap|Line Numbers
  1. // HTML
  2. // the square brackets force an array holding all checked values,
  3. // that is, all names of the input fields you want to work with
  4. <input type="checkbox" name="selection[]" value="example"/> 
  5.  
  6. // the element referenced by the checkbox' value
  7. <input type="text" name="example" ... />
  8.  
  9. // PHP
  10. // always test, that your working variable really exists
  11. if (!isset($_POST['selection'])) return false;
  12.  
  13. // you can use a for or while loop too, depending on your requirement
  14. // loop through all checked checkboxes
  15. foreach ($_POST['selection'] as $selected)
  16. {
  17.     // $selected is the name of the <input> field,
  18.     // $_POST[$selected] is the value of the <input> field
  19.     // whose checkbox was checked
  20.     do_something_with($_POST[$selected]); // this is the placeholder for your code
  21. }
Member
 
Join Date: Oct 2008
Posts: 82
#7: Jan 19 '09

re: How to dynamically create checkboxes in javascript


If I try this am getting the value printed in the php page
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#8: Jan 19 '09

re: How to dynamically create checkboxes in javascript


is that good or bad?
Member
 
Join Date: Oct 2008
Posts: 82
#9: Jan 19 '09

re: How to dynamically create checkboxes in javascript


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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#10: Jan 19 '09

re: How to dynamically create checkboxes in javascript


Quote:

Originally Posted by sarega View Post

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?
Member
 
Join Date: Oct 2008
Posts: 82
#11: Jan 19 '09

re: How to dynamically create checkboxes in javascript


Expand|Select|Wrap|Line Numbers
  1. <?
  2.  $x=array('a','b','c');
  3.  for($i=0;$i<count($x);$i++) {
  4.       echo "<input type='checkbox' id='chk[$i]' onclick=\"list_array($x[$i]\" />$x[$i]<br>";
  5.  }
Member
 
Join Date: Oct 2008
Posts: 82
#12: Jan 19 '09

re: How to dynamically create checkboxes in javascript


in the above code if the checkbox1 is clicked then 'a' has to be passed to the php file. Similarly for other checkboxes
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#13: Jan 19 '09

re: How to dynamically create checkboxes in javascript


put the "a" in the checkbox's value attribute and give the checkbox a name attribute.
Member
 
Join Date: Oct 2008
Posts: 82
#14: Jan 19 '09

re: How to dynamically create checkboxes in javascript


but the number of checkboxes changes dynamically in that case how do i assign that value to the checkbox
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#15: Jan 19 '09

re: How to dynamically create checkboxes in javascript


how do you determine, how many checkboxes you have/need?
Member
 
Join Date: Oct 2008
Posts: 82
#16: Jan 20 '09

re: How to dynamically create checkboxes in javascript


by counting the elements in the array....
Expand|Select|Wrap|Line Numbers
  1. $x=array('a','b','c');
  2. for($i=0;$i<count($x);$i++) {
  3. echo "<input type='checkbox' id='chk[$i]' onclick=\"list_array($x[$i]\" />$x[$i]<br>";
  4. }
  5.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#17: Jan 20 '09

re: How to dynamically create checkboxes in javascript


then your loop should look something like that.....
Expand|Select|Wrap|Line Numbers
  1. $x=array('a','b','c');
  2. for($i=0;$i<count($x);$i++) {
  3.     echo "<input type='checkbox' id='chk[$i]' name='chk[$i]' value='$x[$i]' />$x[$i]<br>";
  4.  }
Reply