Using Checkboxes to Pass Multiple Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Using Checkboxes to Pass Multiple Variables

Post by BigAbe »

Aloha!

I'm in my first few days of PHP/MySQL, and I'm trying to perform multiple actions upon checkboxes being activated for multiple records. I've reviewed severla tutorials, but my needs are more complex than what I've found.

I have a loop which displays the records called by my query plus the following checkbox fields to the side:

Code: Select all

<form action="adminvalidate.php" method="POST">

// later on in my code...

//toggle photo variable
print ("<td align=center><input name=\"togglephoto\" type=\"checkbox\" value=\"$listingID\"></td>");
//remove listing
print ("<td align=center><input name=\"removelisting\" type=\"checkbox\" value=\"$listingID\"></td>");

//further on...

</form>
<input type="submit" name="Submit" value="Update All Ads">
I'm kind of in over my head, and I don't know how to pass these events (of someone checking the boxes) on to the next page and then updating the database for all records on the previous page with the checkbox info.

Any help would be appreciated.

Mahalo!

-- Abe --
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
if ('On' == $_POST['checkboxname'])
{
    //the checkbox is checked, do something with it
}
?>
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

Everah wrote:

Code: Select all

<?php
if ('On' == $_POST['checkboxname'])
{
    //the checkbox is checked, do something with it
}
?>
But how do I handle this for posting multiple records then recieving them on the next page to form the update query?

Thanks,

-- Abe --
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It is a kindacheap hack, but I number my checkboxes like this...

Code: Select all

<input type="checkbox" name="mybox[check_1]" />
<input type="checkbox" name="mybox[check_2]" />
<input type="checkbox" name="mybox[check_3]" />
Then loop through them on the submit page...

Code: Select all

<?php
$mybox = $_POST['mybox'];
for ($i = 0; $i < count($mybox]); $i++)
{
    if ('On' == $mybox['check_' . $i])
    {
        $checkbox . '_' . $i = 1;
    }
    else
    {
        $checkbox . '_' . $i = 0;
    }
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Everah's variable variables are a little bit off.

Code: Select all

${'checkbox_' . $i} = 1;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Thanks Feyd. I threw that one together from memory while 9 girls (ages 9 and under) attacked me with pillows. You gotta love pajama jammie jams...
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

Ok... I'm really lost here...

Sorry for being such a newb, but allow me try a different approach here.

I have a page that executes a SELECT query and displays the results in a nice, pretty table. For each row/record, I have a checkbox that I would like to program to pass on the record number into an array if it is checked.

So here are the steps the user will perform:

1) Load the page
2) View query results (the number displayed I have no control of)
3) Check certain checkboxes (if any)
4) Click submit

Then I just want a list of the records where the checkbox was checked passed onto the next page as an array.

Sorry for being a total newb about this, but I'm trying to be as basic as I can to get this done.

Thanks for your continued patience.

-- Abe --
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<input type="checkbox" name="foo[]" value="someId" />
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

feyd wrote:

Code: Select all

<input type="checkbox" name="foo[]" value="someId" />
So essentially to get this all done, I just need to define the names with some reference to the record in question, and use an loop with those record numbers, to retrieve the appropriate post variables on the next page?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The reference you speak of would be the value, in this case an id number. And you are correct, you could use a loop (although it's not neccesary).

E.g.

Code: Select all

foreach ($_POST['foo'] as $key => $id) {

}
Post Reply