473,770 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling Multiple check boxes

I have a PHP generated page which displays X many records. Each record has
a checkbox preceding it. The user checks several checkboxes, and hits a
delete button. All the corresponding records will be deleted.

But I'm running into difficulty...
Right now the NAME property of each check box is the primary key of the
corresponding record. Hence if I know which checkboxes are checked, I
simply use DELETE using the NAME value.

Generally speaking, how do I get the server side to see which check boxes
were checked?

The check box names may not be sequential, if any records have been deleted
previously, and the first check box might be a number greater than 0.

Is there an easy mechanism to do this? Some kind of built in cnotrol array
allowing me to loop over every check box that was on the form submitted?

I could store the last and first checkbox number in a hidden input, then
loop starting/ending at those values, but that may loop over a lot of
controls that do not exist.
Thoughts?

<Ade
--
Adrian Parker. Ordained priest. <ad***********@ sympatico.ca>

"A society that views graphic violence as entertainment ...should not be
surprised when senseless violence shatters the dreams of it's youngest and
brightest..." - Ensign (March 2004)
Jul 17 '05 #1
13 3567
I've solved this problem in the past by giving each of the checkboxes
the same name, say id, but then setting the value statement to the
primary key I'm on. When you look in your _POST array, you should find
that each id that was checked is in a comma separated list. Just process
this list as you need to loop over the selected primary keys.

Adrian Parker wrote:
I have a PHP generated page which displays X many records. Each record has
a checkbox preceding it. The user checks several checkboxes, and hits a
delete button. All the corresponding records will be deleted.

But I'm running into difficulty...
Right now the NAME property of each check box is the primary key of the
corresponding record. Hence if I know which checkboxes are checked, I
simply use DELETE using the NAME value.

Generally speaking, how do I get the server side to see which check boxes
were checked?

The check box names may not be sequential, if any records have been deleted
previously, and the first check box might be a number greater than 0.

Is there an easy mechanism to do this? Some kind of built in cnotrol array
allowing me to loop over every check box that was on the form submitted?

I could store the last and first checkbox number in a hidden input, then
loop starting/ending at those values, but that may loop over a lot of
controls that do not exist.
Thoughts?

<Ade

--
Martin
(Personal email: ma****@galese.n et)
(Work/Academic email: an**@uchicago.e du)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishab le from the evil that
they set out to destroy."
- Christopher Dawson

"And this gray spirit yearning in desire
To follow knowledge like a sinking star,
Beyond the utmost bound of human thought."
- Lord Alfred Tennyson
Jul 17 '05 #2
"Martin Andrew Galese" <ma****@galese. net> wrote in message
news:rn******** ******@news.uch icago.edu...
I've solved this problem in the past by giving each of the checkboxes
the same name, say id, but then setting the value statement to the
primary key I'm on. When you look in your _POST array, you should find
that each id that was checked is in a comma separated list. Just process
this list as you need to loop over the selected primary keys.
I didn't know the checkbox could have a value. Cool.

The only way I've used the POST array before was by doing things like:
if (isset($_POST["controlnam e"]))

How do you reference it using your method?
Adrian


Adrian Parker wrote:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a
delete button. All the corresponding records will be deleted.

But I'm running into difficulty...
Right now the NAME property of each check box is the primary key of the
corresponding record. Hence if I know which checkboxes are checked, I
simply use DELETE using the NAME value.

Generally speaking, how do I get the server side to see which check boxes were checked?

The check box names may not be sequential, if any records have been deleted previously, and the first check box might be a number greater than 0.

Is there an easy mechanism to do this? Some kind of built in cnotrol array allowing me to loop over every check box that was on the form submitted?

I could store the last and first checkbox number in a hidden input, then
loop starting/ending at those values, but that may loop over a lot of
controls that do not exist.
Thoughts?

<Ade

--
Martin
(Personal email: ma****@galese.n et)
(Work/Academic email: an**@uchicago.e du)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishab le from the evil that
they set out to destroy."
- Christopher Dawson

"And this gray spirit yearning in desire
To follow knowledge like a sinking star,
Beyond the utmost bound of human thought."
- Lord Alfred Tennyson

Jul 17 '05 #3

"Martin Andrew Galese" <ma****@galese. net> wrote in message
news:rn******** ******@news.uch icago.edu...
I've solved this problem in the past by giving each of the checkboxes
the same name, say id, but then setting the value statement to the
primary key I'm on. When you look in your _POST array, you should find
that each id that was checked is in a comma separated list. Just process
this list as you need to loop over the selected primary keys.
That doesn't seem to work.

foreach (array_keys($_P OST) as $key) {
$$key = $_POST[$key];
print "$key is ${$key}<br />";
}
When I name all the textboxes the same, and make their values the primary
key of the record to delete, the _POST array just contains the total number
of checked checkboxes.

Output:
"deleteMe is 8
Delete is Delete selected"

I check 8 check boxes. Their names are 1, 2, 3, 4, 5, 6, 7, 8

Either _POST is getting the number of how many are checked, or the value of
the last one only.

Adrian

Adrian Parker wrote:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a
delete button. All the corresponding records will be deleted.

But I'm running into difficulty...
Right now the NAME property of each check box is the primary key of the
corresponding record. Hence if I know which checkboxes are checked, I
simply use DELETE using the NAME value.

Generally speaking, how do I get the server side to see which check boxes were checked?

The check box names may not be sequential, if any records have been deleted previously, and the first check box might be a number greater than 0.

Is there an easy mechanism to do this? Some kind of built in cnotrol array allowing me to loop over every check box that was on the form submitted?

I could store the last and first checkbox number in a hidden input, then
loop starting/ending at those values, but that may loop over a lot of
controls that do not exist.
Thoughts?

<Ade

--
Martin
(Personal email: ma****@galese.n et)
(Work/Academic email: an**@uchicago.e du)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishab le from the evil that
they set out to destroy."
- Christopher Dawson

"And this gray spirit yearning in desire
To follow knowledge like a sinking star,
Beyond the utmost bound of human thought."
- Lord Alfred Tennyson

Jul 17 '05 #4

"Martin Andrew Galese" <ma****@galese. net> wrote in message
news:rn******** ******@news.uch icago.edu...
I've solved this problem in the past by giving each of the checkboxes
the same name, say id, but then setting the value statement to the
primary key I'm on. When you look in your _POST array, you should find
that each id that was checked is in a comma separated list. Just process
this list as you need to loop over the selected primary keys.
I feel supid now.

This doesn't work. If 10 check boxes all have the same name, the only value
used is that of that last one. They cascadingly change the value of the one
checkbox name.


Adrian Parker wrote:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a
delete button. All the corresponding records will be deleted.

But I'm running into difficulty...
Right now the NAME property of each check box is the primary key of the
corresponding record. Hence if I know which checkboxes are checked, I
simply use DELETE using the NAME value.

Generally speaking, how do I get the server side to see which check boxes were checked?

The check box names may not be sequential, if any records have been deleted previously, and the first check box might be a number greater than 0.

Is there an easy mechanism to do this? Some kind of built in cnotrol array allowing me to loop over every check box that was on the form submitted?

I could store the last and first checkbox number in a hidden input, then
loop starting/ending at those values, but that may loop over a lot of
controls that do not exist.
Thoughts?

<Ade

--
Martin
(Personal email: ma****@galese.n et)
(Work/Academic email: an**@uchicago.e du)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishab le from the evil that
they set out to destroy."
- Christopher Dawson

"And this gray spirit yearning in desire
To follow knowledge like a sinking star,
Beyond the utmost bound of human thought."
- Lord Alfred Tennyson

Jul 17 '05 #5
In article <24************ *******@news20. bellglobal.com> ,
"Adrian Parker" <ad***********@ NOSPAMsympatico .ca> wrote:
Is there an easy mechanism to do this? Some kind of built in cnotrol array
allowing me to loop over every check box that was on the form submitted?


Make the "name" attribute an array.

<input type="checkbox" name="checkbox_ id[1]" value="a" />
<input type="checkbox" name="checkbox_ id[3]" value="b" />
<input type="checkbox" name="checkbox_ id[56]" value="c" />
<input type="checkbox" name="checkbox_ id[145]" value="d" />

If a and c are checked, then after submitting $_REQUEST['checkbox_id']
is an array containing this: [1] => a [56]=> d.

JP

--
Sorry, <de*****@cauce. org> is een "spam trap".
E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.
Jul 17 '05 #6

"Jan Pieter Kunst" <de*****@cauce. org> wrote in message
news:de******** *************** ****@news1.news .xs4all.nl...
In article <24************ *******@news20. bellglobal.com> ,
"Adrian Parker" <ad***********@ NOSPAMsympatico .ca> wrote:
Is there an easy mechanism to do this? Some kind of built in cnotrol array allowing me to loop over every check box that was on the form submitted?


Make the "name" attribute an array.

<input type="checkbox" name="checkbox_ id[1]" value="a" />
<input type="checkbox" name="checkbox_ id[3]" value="b" />
<input type="checkbox" name="checkbox_ id[56]" value="c" />
<input type="checkbox" name="checkbox_ id[145]" value="d" />

If a and c are checked, then after submitting $_REQUEST['checkbox_id']
is an array containing this: [1] => a [56]=> d.


How can I loop through it without knowing how many elements it holds?

Example code?
Adrian
Jul 17 '05 #7
In article <Y0************ *******@news20. bellglobal.com> ,
"Adrian Parker" <ad***********@ NOSPAMsympatico .ca> wrote:
Make the "name" attribute an array.

<input type="checkbox" name="checkbox_ id[1]" value="a" />
<input type="checkbox" name="checkbox_ id[3]" value="b" />
<input type="checkbox" name="checkbox_ id[56]" value="c" />
<input type="checkbox" name="checkbox_ id[145]" value="d" />

If a and c are checked, then after submitting $_REQUEST['checkbox_id']
is an array containing this: [1] => a [56]=> d.

(Should be [56] => c, sorry)
How can I loop through it without knowing how many elements it holds?


That's what foreach() is for! See
<http://nl.php.net/manual/en/control-structures.fore ach.php>.

Assuming that the form was submitted with method="post":

foreach($_POST[checkbox_id] as $record_id => $value) {

do_something_in _database($reco rd_id);
...
}

JP

--
Sorry, <de*****@cauce. org> is een "spam trap".
E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.
Jul 17 '05 #8

"Jan Pieter Kunst" <de*****@cauce. org> wrote in message
news:de******** *************** ****@news1.news .xs4all.nl...
In article <24************ *******@news20. bellglobal.com> ,
"Adrian Parker" <ad***********@ NOSPAMsympatico .ca> wrote:
Is there an easy mechanism to do this? Some kind of built in cnotrol array allowing me to loop over every check box that was on the form submitted?


Make the "name" attribute an array.

<input type="checkbox" name="checkbox_ id[1]" value="a" />
<input type="checkbox" name="checkbox_ id[3]" value="b" />
<input type="checkbox" name="checkbox_ id[56]" value="c" />
<input type="checkbox" name="checkbox_ id[145]" value="d" />

If a and c are checked, then after submitting $_REQUEST['checkbox_id']
is an array containing this: [1] => a [56]=> d.


This seems to work:

foreach(array_k eys($_POST["checkBox_Delet e"]) as $key)
{
if ($deleteCount == 0)
{
$deleteThese = "'" . $key . "'";
$deleteCount += 1;
} else {
$deleteThese .= ", '" . $key . "'";
}
}
This look good to you?
Adrian
Jul 17 '05 #9
In article <qk************ *******@news20. bellglobal.com> ,
"Adrian Parker" <ad***********@ NOSPAMsympatico .ca> wrote:
This seems to work:

foreach(array_k eys($_POST["checkBox_Delet e"]) as $key)
{
if ($deleteCount == 0)
{
$deleteThese = "'" . $key . "'";
$deleteCount += 1;
} else {
$deleteThese .= ", '" . $key . "'";
}
}
This look good to you?


If you need the number of checked checkboxes, it is faster to get that
like this:

$deleteCount = count($_POST['checkBox_Delet e']);

JP

--
Sorry, <de*****@cauce. org> is een "spam trap".
E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1723
by: Pax S | last post by:
I need a button that will check (make true) two check boxes (fields) For the record that has the focus (the record on the form that I am presently looking at). The two check boxes would be like a "sold" check box and an "add to report" check box (for print out). Also as part of this button, I would like for it to enter a date into a field such as a "date sold" field. That should be fairly simple and I could probably figure out how to do...
1
3101
by: Jim in Arizona | last post by:
I'm having dificulty figuring out how to process multiple check boxes on a web form. Let's say I have three check boxes: cbox1 cbox2 cbox3 The only way I can think of to code the possibilities is something like:
14
1599
by: Xero | last post by:
Hello. I am using Visual Studio .NET (Academic Edition) to write a VB program. My computer is running Win XP Pro. I am writing a calculator and requires users to enter two numbers. After entering the numbers, they should click a button called 'Display Results'. How can I display a dialog box prompting the users to check their entry if they left any of the boxes empty? I have tried the .IsNaN function but in vein. Thanks.
7
3113
by: Siv | last post by:
Hi, I have an MDI application that uses a generic "ShowPage" routine in a module that is called when I want to display a child form. The basic idea is that in the module I have declared each form as follows: Friend F0 As frmMain Friend F1 As frmStart Friend F2 As frmSearch Then in my ShowPage routine (which is passed a string "pageToShow" which is the name of the form I wish to open), I first check to see if we already have an instance...
10
15977
by: Jim in Arizona | last post by:
I'm having dificulty figuring out how to process multiple check boxes on a web form. Let's say I have three check boxes: cbox1 cbox2 cbox3 The only way I can think of to code the possibilities is something like:
6
6899
by: Harshpandya | last post by:
Hi all, I am working on the form in which you fill out the whole PHP form and e mail that details to someone. It is working fine. But now i want to send the same form to be sent to different people and user should be able choose the check boxes to whom they want to send e mail to. I write some code simple If else conditions but i think i am making some mistakes. Because when i tried - it is not sending them e mails. Here is my code. I...
3
2699
by: Zabto | last post by:
I created an array of 8X5 boxes to represents pixels in a character. Each box is a custom UserControl of a fixed size which is filled black if it's activated, or white if it's not. I wanted to change multiple boxes with a click and drag. On mouse down I toggle the value of the current box and then set two static variables for the class: ButtonDown = true; Activate = (true or false, depending upon which action was just taken.) The...
6
4673
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option for 2 list boxes:- county and nationality, while trying to add a third multi select list box for qualifications search is where i encounter my problem. I've copied the working code from my working list boxes, however it cant seem to pick up the...
1
6799
by: woodey2002 | last post by:
Hi Everyone and many thanks for your time.. I am trying to begin access and a bit of VBA i am enjoying it but I have a annoying problem I just can’t get any where on. My databse mostly includes bits of code for different examples. I have one last thing to finish. I am trying to create a search form that will allow users to select criteria from multiple sources eg ,multi select list boxes , combo boxes. I have a subform showing all the...
0
10228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.