Selecting Multiple Checkboxes question

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
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Selecting Multiple Checkboxes question

Post by michlcamp »

This problem involves selecting multiple orders using checkboxes next to each item on a list of customer names (which was created from a MySQL query) so that the items selected can be moved to an "archived orders" table.

For simplicity's sake, let's say the list is of ten customer names.

My query results give me a page with a list of orders that have come in today, with a checkbox to select the orders I want to process and then archive.

my WHILE loop echoes this result:

Code: Select all

<input type=checkbox value="$order_id"> $customer_name </br>

I want to select five of the ten orders, move them to a database table named "archived", then remove them from the original table.

Looking for anyone that can point me in the right direction....
Thanks in advance.
mc
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

What are you having trouble with specifically? Do you just need help getting them into an array? If so try naming them like this

<input type="checkbox" name="checkbox[]" value="1" />

and PHP should make an array called $_REQUEST['checkbox'] which is a numerically indexed array, you get them back in the same order they were on the page
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

(thanks for your quick response).

Well, this is a new one for me, so I'm hoping I can find a tutorial or get enough info here to be able to write enough code to start working with the array that's created by my selections.

I don't know how to write the code that interprets the "$_REQUEST['checkbox'] " array once I get it...

Maybe what would help most is to know how to POST the selected items (array) to the next page using a form call and submit button....

something like:

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

<input type="checkbox" name="checkbox[]" value="1" /> $customer_name

<input type="submit">
</form>

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

Post by John Cartwright »

You should have your page build the checkboxes putting the ids in the value attribute

Code: Select all

<?php 

//if orderid contains anything and is an array
if (!empty($_POST['orderid']) && is_array($_POST['orderid'])) {
   //loop through orderid's
   foreach ($_POST['orderid'] as $orderid) {
      echo 'Order #'.$orderid.' was checked <br />';
   }
} 

//simulate data coming from database
$orders = array(
   001 => 'John', 
   002 => 'Jane', 
   003 => 'Jones', 
   004 => 'Jack'
);

echo '<form action="archive.php" method="POST">';

//build textboxes from database information
foreach ($orders as $orderid => $customer) {
   echo '<input type="checkbox" name="orderid[]" value="'.$orderid.'" /> '.$customer_name .'<br />';
}

echo '<input type="submit">';
echo '</form>';
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

Post by namitjung »

Code: Select all

<input type="checkbox" name="customerid[]" value = "<?php $customerid ?>"/>
Remember [] in name field. This makes customerid as an array which can hold different customer id values.When form is submitted through post or get method this values is passed as an array in the php file.You can always use this value in the same way you use an array. You can get this value in the simar way you get other values in php.Like this way

Code: Select all

$customerid_array = $_POST['customerid'];
Now customerid_array variable will hold all the values passed from form as an array.

But if you forgot to use [] in name field this will led php to understand it as a single variable,which will store the only one customerid value. So when you submit the form it will pass only one last customerid value in the php script.


Happy Programming :D
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Use this as an example (it took a working example for it to finally clikc for me, so maybe others will need it as well :))

Code: Select all

<?php 

if (!isset($_POST['chkbox'])) {
echo <<<PAGE
<html>
<body>
<form action="" method="post">
    <input type="checkbox" name="chkbox[]" value="1" /><br />
    <input type="checkbox" name="chkbox[]" value="2" /><br />
    <input type="checkbox" name="chkbox[]" value="3" /><br />
    <input type="checkbox" name="chkbox[]" value="4" /><br />
    <input type="checkbox" name="chkbox[]" value="5" /><br />
    <input type="checkbox" name="chkbox[]" value="6" /><br />
    <input type="checkbox" name="chkbox[]" value="7" /><br />
    <input type="checkbox" name="chkbox[]" value="8" /><br />
    <input type="checkbox" name="chkbox[]" value="9" /><br />
    <input type="checkbox" name="chkbox[]" value="10" /><br />
    <input type="submit" />
</form>
</body>
</html>
PAGE;

} else {

    foreach ($_POST['chkbox'] as $chkbox) {
        echo "Value: {$chkbox} was selected!<br />\n";
    }

}

?>
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

Seems great... but the number of checkboxes changes based on how many rows are in the 'orders' table. Once the orders have been shipped, they're moved out of that table into another ('archived'), so one morning there might be ten new orders, the next day there could be seven...
Maybe I'm missing something...
thanks.
mc
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Try clicking various different boxes on the example I posted, and see the outcome :)

You can do count($_POST['chkbox']) to get the total number of checkboxes that share that name, that have been checked :)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Seems great... but the number of checkboxes changes based on how many rows are in the 'orders' table.
mc, If I understand correctly what you want:
You can combine jcart's loop to retreive and show the necessary checkboxes with the last example Jenk gave. Then everything is dynamic. The amount of checkboxes shown depends on your db query and the amount of POST values received depends on the amount of checkboxes clicked.

Isn't that what you want?
Post Reply