Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Multiple Select (Checkbox)?

    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
    1,1
    1,2

    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).

    Thanks!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    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 ...

    $toppings=$_POST['toppings'];
    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="";
    $t2
    $t3
    $t4
    etc.

    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.


    .
    Last edited by mlseim; 11-17-2011 at 02:54 AM.

  • Users who have thanked mlseim for this post:

    ontheedge (11-17-2011)

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    edit.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •