473,320 Members | 1,848 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,320 software developers and data experts.

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 3532
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.net)
(Work/Academic email: an**@uchicago.edu)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishable 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.uchicago.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["controlname"]))

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.net)
(Work/Academic email: an**@uchicago.edu)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishable 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.uchicago.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($_POST) 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.net)
(Work/Academic email: an**@uchicago.edu)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishable 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.uchicago.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.net)
(Work/Academic email: an**@uchicago.edu)

"As soon as men decide that all means are permitted to fight an evil,
then their good becomes indistinguishable 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.xs4a ll.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.foreach.php>.

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

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

do_something_in_database($record_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.xs4a ll.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_keys($_POST["checkBox_Delete"]) 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_keys($_POST["checkBox_Delete"]) 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_Delete']);

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #10
On Thu, 29 Apr 2004 22:14:59 +0200
Jan Pieter Kunst <de*****@cauce.org> wrote:
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.foreach.php>.

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

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

do_something_in_database($record_id);
...
}


You can also do

reset($array);

while( list($key,$val) = each( $array ) ) {

echo $key . ' => ' . $val;

}
Jul 17 '05 #11

"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:de***************************@news1.news.xs4a ll.nl...
In article <qk*******************@news20.bellglobal.com>,
"Adrian Parker" <ad***********@NOSPAMsympatico.ca> wrote:
This seems to work:

foreach(array_keys($_POST["checkBox_Delete"]) 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_Delete']);


Nah, the deleteCount is just used to see if it's the first element to be
added to the string deleteCount.

deleteCount is building a single quote and comma delimited string that will
be used as argument for a mySQL IN function.

Adrian
Jul 17 '05 #12
In article <1C*******************@news20.bellglobal.com>,
"Adrian Parker" <ad***********@NOSPAMsympatico.ca> wrote:
Nah, the deleteCount is just used to see if it's the first element to be
added to the string deleteCount.

deleteCount is building a single quote and comma delimited string that will
be used as argument for a mySQL IN function.


Ah, I see. In that case this will build the querystring in less code:

$querystring = "DELETE FROM ... WHERE ... IN ('" . join("','",
array_keys($_POST['checkbox_delete']) . "')";

JP

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

"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:de***************************@news1.news.xs4a ll.nl...
In article <1C*******************@news20.bellglobal.com>,
"Adrian Parker" <ad***********@NOSPAMsympatico.ca> wrote:
Nah, the deleteCount is just used to see if it's the first element to be
added to the string deleteCount.

deleteCount is building a single quote and comma delimited string that will be used as argument for a mySQL IN function.


Ah, I see. In that case this will build the querystring in less code:

$querystring = "DELETE FROM ... WHERE ... IN ('" . join("','",
array_keys($_POST['checkbox_delete']) . "')";


Cool, thanks.
Adrian
Jul 17 '05 #14

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

Similar topics

1
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...
1
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...
14
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...
7
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...
10
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...
6
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...
3
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...
6
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...
1
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
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...
0
isladogs
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...
1
isladogs
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
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 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.