473,387 Members | 3,781 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,387 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 6278
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.