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 6275
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: 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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |