Handle Multiple Check Boxes?

This is a discussion on Handle Multiple Check Boxes? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-28-2004
Adrian Parker
 
Posts: n/a
Default Handle 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. <adrian.parker@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)


Reply With Quote
  #2 (permalink)  
Old 04-29-2004
Milambar
 
Posts: n/a
Default Re: Handle Multiple Check Boxes?

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

generate the list with something like this:

$result = mysql_query("SELECT * FROM table", $fh);
while ($row = mysql_fetch_array($result)) {
echo "<input name="id[]" type="check value="$row[0]"><br>";
}

then when it gets passed to the serverside, $_POST['id'] will be an
array containing the values of the checked boxes. Just walk through the
array deleting them one by one.

Off the top if my head without testing:

$delete = $_POST['id'];
foreach ($delete as $this) {
mysql_query("delete from table where id=$this");
}

I hope this helps, and btw, it took me a while to understand how
checkboxes were handled too.
Reply With Quote
  #3 (permalink)  
Old 04-29-2004
Milambar
 
Posts: n/a
Default Re: Handle Multiple Check Boxes?

Milambar wrote:

> 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

>
> generate the list with something like this:
>
> $result = mysql_query("SELECT * FROM table", $fh);
> while ($row = mysql_fetch_array($result)) {
> echo "<input name="id[]" type="check value="$row[0]"><br>";
> }
>
> then when it gets passed to the serverside, $_POST['id'] will be an
> array containing the values of the checked boxes. Just walk through the
> array deleting them one by one.
>
> Off the top if my head without testing:
>
> $delete = $_POST['id'];
> foreach ($delete as $this) {
> mysql_query("delete from table where id=$this");
> }
>
> I hope this helps, and btw, it took me a while to understand how
> checkboxes were handled too.

Meh, dont forget to escape the quotes in the generation part:
echo "<input name=\"id[]\" type=\"checkbox\" value=\"$row[0]\"><br>";

Sorry. But it is 3am here at the moment.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:20 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0