Page 1 of 1
Using Checkboxes to Pass Multiple Variables
Posted: Fri Mar 31, 2006 7:53 pm
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 --
Posted: Fri Mar 31, 2006 7:55 pm
by RobertGonzalez
Code: Select all
<?php
if ('On' == $_POST['checkboxname'])
{
//the checkbox is checked, do something with it
}
?>
Posted: Fri Mar 31, 2006 8:14 pm
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 --
Posted: Fri Mar 31, 2006 8:50 pm
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;
}
}
?>
Posted: Fri Mar 31, 2006 10:02 pm
by feyd
Everah's variable variables are a little bit off.
Posted: Sat Apr 01, 2006 9:54 am
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...
Posted: Fri Apr 07, 2006 9:28 pm
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 --
Posted: Fri Apr 07, 2006 10:04 pm
by feyd
Code: Select all
<input type="checkbox" name="foo[]" value="someId" />
Posted: Sat Apr 22, 2006 7:44 pm
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?
Posted: Sat Apr 22, 2006 7:50 pm
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) {
}