View Full Version : Multiple Select (Checkbox)?

11-17-2011, 12:37 AM
I've searched all over the internet and finally decided to post because I just can't figure this out :(

I'll try to give a pretty simple example and hopefully be able to describe what I need:

Example: Let's say I have to store information on Pizza's and their toppings in a database. There would be a Pizza Table (Unique ID and Description). An example might be: 1, Meat Pizza {Where 1 is the UID and Supreme is the description of the pizza type}.

There would allso be a Toppings Table (Unique ID and Description). An example might be: 1, Pepperoni... 2, Sausage... 3, Green Peppers. (Same style as above).

There is a 3rd table, let's say: Pizza_Toppings. This would contain the Unique ID's from the 2 tables above... So if the meat pizza had Pepperoni and Sausage only, it would look like this:

Pizza_Topings Datatable
Pizza_UID, Toppings_UID

That would show that the Meat Pizza (UID=1) has both Pepperoni (UID=1) and Sausage (UID=2)

Here is the problem (sorry it took so long to get here) :)

I want to be able to edit this information and was hoping to have checkboxes for ALL ingredients. Then I would either check or uncheck depending upon need for that pizza. The problem comes when I uncheck something.. I can't seem to 'pass through' both checked=yes and checked=no in the _POST.

What is the BEST way to do this (even if it means another object, not a checkbox).


11-17-2011, 02:51 AM
Checkboxes are funky with PHP.

You have a checkbox array in your form ...
the names are all the same (with the [] indicating an array),
and the values are unique.

Start with this example ....

<input type="checkbox" name="toppings[]" value="Pepperoni">
<input type="checkbox" name="toppings[]" value="Sausage">
<input type="checkbox" name="toppings[]" value="Anchovies">
<input type="checkbox" name="toppings[]" value="Mushrooms">

When you process this in your PHP script,
you'll read in the array ...

foreach($toppings as $topping){
echo $topping."<br>";

You'll notice that the array ONLY contains the ones that are checked.
I'm not sure how your database is set up, but you'll save the ones that are checked.
First, make all of the topping spots in your database = 0 (no toppings at all).
Then, as you loop through the checkbox array, you'll only see the ones the user checked.
If the topping is in the checkbox array list, make it a 1.

The database updates, and later on, they go back to the form ...

Now, to read them back.

At the top of your script that contains the form, you'll query your database
and determine which toppings are checked.

Set a unique variable for each topping ...
If it's checked ... $t1="checked"
If it's not ... $t1="";

Modify your form now to put the correct $t number on the proper topping line:

<input type="checkbox" name="toppings[]" value="Pepperoni" <?=$t1?>>
<input type="checkbox" name="toppings[]" value="Sausage" <?=$t2?>>
<input type="checkbox" name="toppings[]" value="Anchovies" <?=$t3?>>
<input type="checkbox" name="toppings[]" value="Mushrooms" <?=$t4?>>

So your form would now look like that.
It will check only the ones that were a value of 1 (in your database).

The cycle repeats when they submit it again.


11-17-2011, 02:59 AM
ah..... stupid me.

I had the array of checkboxes already and the return, but it only returned the 'checked' items. I kept concentrating on trying to return the non-checked instead of reconciling that with my database!

That was a rookie mistake!

Thanks a ton!

11-17-2011, 03:24 AM