Page 1 of 1

Handling Multiple checkboxes in a form

Posted: Thu Nov 30, 2006 7:05 pm
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;
?>

Posted: Thu Nov 30, 2006 8:31 pm
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.

Posted: Fri Dec 01, 2006 11:04 am
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)){

 ?>

Posted: Fri Dec 01, 2006 11:08 am
by Burrito
have a look at foreach()

Posted: Fri Dec 01, 2006 11:55 am
by sparky753
Would I just do this?

Code: Select all

foreach($_POST['checkbox']){
I'm sorry, I haven't worked extensively with PHP arrays...

Posted: Fri Dec 01, 2006 11:58 am
by Burrito

Code: Select all

foreach($_POST['checkbox'] as $val)
   echo $val;

Posted: Fri Dec 01, 2006 12:04 pm
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)

Posted: Fri Dec 01, 2006 12:05 pm
by Burrito
The Ninja Space Goat wrote:It's also just considered better practice to put the curly braces there....
8O

Posted: Fri Dec 01, 2006 12:06 pm
by Luke
no? :?

Posted: Fri Dec 01, 2006 12:07 pm
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

Posted: Fri Dec 01, 2006 12:08 pm
by John Cartwright
The Ninja Space Goat wrote:no? :?
in your opinion maybe :wink: , but lets avoid this discussion 8O

Posted: Fri Dec 01, 2006 12:11 pm
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

Posted: Fri Dec 01, 2006 12:19 pm
by sparky753
Thanks so much!!! I really appreciate it...