Handling Multiple checkboxes in a form

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
sparky753
Forum Commoner
Posts: 51
Joined: Fri Nov 03, 2006 10:39 am

Handling Multiple checkboxes in a form

Post by sparky753 »

I have a form on a PHP page which generates a list of products with their descriptions from a database. Beside each product, a checkbox is generated dynamically:

Code: Select all

<tr>
     <td align="center" ><input name="checkbox[]" type="checkbox" value="<?= $row['coffeeID'] ?>"></td>
                    <td><?= $row['price'] ?></td>
                    <td><?= $row['coffeeName'] ?></td>
                    <td><?= $row['coffeeDesc'] ?></td>
                    <td><?= $row['region'] ?></td>
   </tr>
Whenever a checkbox is selected and the form is submitted, the new page should display the value of the checkbox selected. I have no problem displaying a checkbox value when only one checkbox has been selected but how do i display multiple selections on the new page?

This is my code on the new page:

Code: Select all

<?php
$checkboxes = $_POST['checkbox[]'];
echo $checkboxes;
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

echo '<pre>';
print_r($_POST['checkbox']);
echo '</pre>';
Which means $_POST['checkbox'] is an array itself of the checkbox values.
sparky753
Forum Commoner
Posts: 51
Joined: Fri Nov 03, 2006 10:39 am

Post by sparky753 »

Awesome!! Thanks so much...how would i use each value? For instance, I'm trying to use each value(which corresponds to coffeeID) in a SQL statement:

Code: Select all

<?php
  $query1 = "SELECT * FROM tblCoffee WHERE coffeeID = '{$_POST['checkbox']}' order by coffeeName";

  $result1 = @mysql_query ($query1);
                				
     while ($row1 = mysql_fetch_array($result1)){

 ?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

have a look at foreach()
sparky753
Forum Commoner
Posts: 51
Joined: Fri Nov 03, 2006 10:39 am

Post by sparky753 »

Would I just do this?

Code: Select all

foreach($_POST['checkbox']){
I'm sorry, I haven't worked extensively with PHP arrays...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

foreach($_POST['checkbox'] as $val)
   echo $val;
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Just to clarify:

This syntax works fine, and for this particular instance, it would be ok,

Code: Select all

foreach($_POST['checkbox'] as $val)
   echo $val;
but if you wanted to put more than one line of code in the loop, you'd need to enclose the block in curly braces

Code: Select all

foreach($_POST['checkbox'] as $val)
{
    echo $val;
    echo " is my favorite value.";
}
It's also just considered better practice to put the curly braces there (leaving them out with the one-liners can sometimes lead to bugs when you add more lines and forget the braces)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

The Ninja Space Goat wrote:It's also just considered better practice to put the curly braces there....
8O
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

no? :?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

implode() may also be of interest.. you also want to make sure the check boxes were actually filled out.

Code: Select all

if (!empty($_POST['checkbox'] && count($_POST['checkbox'])) 
{
   $query1 = 'SELECT * FROM tblCoffee WHERE coffeeID IN ('. implode(',', $_POST['checkbox']).') order by coffeeName';

   //outputs something like
   //SELECT * FROM tblCoffee WHERE coffeeID IN (1, 4, 6, 10) order by coffeeName
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The Ninja Space Goat wrote:no? :?
in your opinion maybe :wink: , but lets avoid this discussion 8O
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

It's a matter of preference.

I don't think it's fair to say it's 'better practice' to include them.

for writing and reading code, it's easier if only one operation needs to take place to not include them. When reading code, it's an immediate identifier that only the next line matters, which simplifies understanding. When writing code, the same rule applies, plus you don't have to hit the shift key on your keyboard twice :P
sparky753
Forum Commoner
Posts: 51
Joined: Fri Nov 03, 2006 10:39 am

Post by sparky753 »

Thanks so much!!! I really appreciate it...
Post Reply