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
16 6278
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]);
-
}
are you telling that the javascript is not required at all?? then how to accept the users input of checking a checkbox?
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
can you please give me example of it?
@sarega
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
-
}
If I try this am getting the value printed in the php page
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
@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?
- <?
-
$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>";
-
}
in the above code if the checkbox1 is clicked then 'a' has to be passed to the php file. Similarly for other checkboxes
put the "a" in the checkbox's value attribute and give the checkbox a name attribute.
but the number of checkboxes changes dynamically in that case how do i assign that value to the checkbox
how do you determine, how many checkboxes you have/need?
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>";
-
}
-
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>";
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |