473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to dynamically create checkboxes in javascript

82
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
Jan 16 '09 #1
16 6275
Dormilich
8,658 Expert Mod 8TB
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. }
Jan 16 '09 #2
sarega
82
are you telling that the javascript is not required at all?? then how to accept the users input of checking a checkbox?
Jan 16 '09 #3
Dormilich
8,658 Expert Mod 8TB
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
Jan 16 '09 #4
sarega
82
can you please give me example of it?
Jan 19 '09 #5
Dormilich
8,658 Expert Mod 8TB
@sarega
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. }
Jan 19 '09 #6
sarega
82
If I try this am getting the value printed in the php page
Jan 19 '09 #7
Dormilich
8,658 Expert Mod 8TB
is that good or bad?
Jan 19 '09 #8
sarega
82
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
Jan 19 '09 #9
Dormilich
8,658 Expert Mod 8TB
@sarega
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?
Jan 19 '09 #10
sarega
82
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.  }
Jan 19 '09 #11
sarega
82
in the above code if the checkbox1 is clicked then 'a' has to be passed to the php file. Similarly for other checkboxes
Jan 19 '09 #12
Dormilich
8,658 Expert Mod 8TB
put the "a" in the checkbox's value attribute and give the checkbox a name attribute.
Jan 19 '09 #13
sarega
82
but the number of checkboxes changes dynamically in that case how do i assign that value to the checkbox
Jan 19 '09 #14
Dormilich
8,658 Expert Mod 8TB
how do you determine, how many checkboxes you have/need?
Jan 19 '09 #15
sarega
82
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.  
Jan 20 '09 #16
Dormilich
8,658 Expert Mod 8TB
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.  }
Jan 20 '09 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff Johnson | last post by:
Hi, Does anyone know how I would code a dynamically generated CheckBoxList within a PlaceHolder? I have a function that returns an array, I then want to loop throuth the array and create...
2
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the...
2
by: somaskarthic | last post by:
Hi In my php code , i dynamically created table rows on button click event. Each row contain 3 selectboxes, 7 checkboxes . On each click of these elements i need to submit the form and save the...
4
by: somaskarthic | last post by:
Hi I posted query about dynamically generating php code. Sorry for the inconvenience . I didn't given you the php code. Its just javascript code which dynamically create rows on button click event....
8
by: SalamElias | last post by:
I create 2 groupes of checkboxes dynamically (I don't use checkboxlist) for 2 differnet zones in my aspx web page and I assign a "onClick" javascript handler by group., lets say the first 4 chboxes...
1
by: Petyr David | last post by:
Current: web page has hard coded directories (using checkboxes) user may choose to search for files matching a pattern they enter in text box. Perl is used to search for files and to format HTML...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
9
by: Sebarry | last post by:
Hi, I've put together a little test page to create checkboxes each time a button is clicked. The check button is created and should be appended to the form, but when the post is submitted the...
3
by: N L | last post by:
Greetings, I want to create a form in Access that shows a list of checkboxes to a user. The checkboxes will be dynamically generated, showing the fields in a table the user has selected in a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.