Forum Moderators: coopster

Message Too Old, No Replies

Processing checkboxes into SQL queries

And doing more than one at once.

         

cartographer

5:18 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



Hi, I'm not amazingly experienced at PHP, but I've got some experience. This one thing has me a little bit stumped though.

Basically, it goes like this:
[ ] User 1
[ ] User 2
[ ] User 3

[ Remove users ]

The top three parts are checkboxes that the user can check/uncheck (as you'd expect), the bottom is a button the user presses to thus remove them (I have a separate form for adding them that already works).

I would presume that the results of which boxes are checked will be returned as an array. What I need to then do is then count the number of variables in that array and run multiple queries (updates, deletes, etc.) for each.

Thanks in advance!

d40sithui

7:06 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



Yes, it's completely possible to set it up that way. The key thing here is to name your checkbox name with brackets.

<inside your form>


<input type="checkbox" name="users[]" value="1">
<input type="checkbox" name="users[]" value="2">
<input type="checkbox" name="users[]" value="3">
<input type="checkbox" name="users[]" value="x">

<inside php action script>


$users = $_POST['users'];
foreach($users as $user){
//$user now contains 1, 2, 3, or x
//if any of them were checked they will show up in this loop
//this is where you will process this information and run your queries
}

//print_r($_POST); //when you get lost

The value of each checkbox should be your user IDs to make this work. For the purpose of simplicity, I used static values.