How to dynamically create checkboxes in javascript | Member | | Join Date: Oct 2008
Posts: 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. -
<?
-
$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: -
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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | 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? - // 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]);
-
}
| | Member | | Join Date: Oct 2008
Posts: 82
| | | 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | 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
| | | re: How to dynamically create checkboxes in javascript
can you please give me example of it?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: How to dynamically create checkboxes in javascript Quote:
Originally Posted by sarega can you please give me example of it? see post #2. - // 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
-
}
| | Member | | Join Date: Oct 2008
Posts: 82
| | | re: How to dynamically create checkboxes in javascript
If I try this am getting the value printed in the php page
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: How to dynamically create checkboxes in javascript
is that good or bad?
| | Member | | Join Date: Oct 2008
Posts: 82
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: How to dynamically create checkboxes in javascript Quote:
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?
| | Member | | Join Date: Oct 2008
Posts: 82
| | | re: How to dynamically create checkboxes in javascript - <?
-
$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>";
-
}
| | Member | | Join Date: Oct 2008
Posts: 82
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | 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
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | 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
| | | re: How to dynamically create checkboxes in javascript
by counting the elements in the array.... -
$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>";
-
}
-
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: How to dynamically create checkboxes in javascript
then your loop should look something like that..... - $x=array('a','b','c');
-
for($i=0;$i<count($x);$i++) {
-
echo "<input type='checkbox' id='chk[$i]' name='chk[$i]' value='$x[$i]' />$x[$i]<br>";
-
}
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|