#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    4
    Rep Power
    0
    I am a beginner in PHP programming and need your help.
    I have a HTML form that contains checkboxes.
    Let's say you are ordering a Hamburger. You need to thick boxes for additional toppings (ie pickles, onions etc)
    At the end ths order will be written to a file. How can I get the PHP to recognise whether which boxes are checked?
    Thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    114
    Rep Power
    15
    PHP automatically exposes variables posted from an HTML form to your script. Eg:

    <form action="some.php" method="post">
    <input type="checkbox" name="pickles" value="pickles are an awesome topping for burgers">
    <input type="submit" value="dang pickles are dope">
    </form>

    and then in some.php...

    echo $pickles;
    // should show the value you assigned to the
    // check box named pickles.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Antwerp, Belgium
    Posts
    2
    Rep Power
    0
    say you had this in your html:

    <form action="do.php" method="post">
    <input type="checkbox" name="pickles" value="1">pickles
    <input type="ketchup" name="ketchup" value="1">ketchup
    </form>

    then you could do this in your do.php:

    if ($pickles == 1) {
    putpicklesonbuger();
    }

    if ($ketchup == 1) {
    slaponsomeketchup();
    }


    you can always directly use the name of form variables in php.
    
  6. #4
  7. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    Okay, well let's take this up a notch.

    Using your "hamburger" example, let me throw this into the mix.

    <input type="checkbox" name="toppings[]" value="pickles">
    <input type="checkbox" name="toppings[]" value="onions">
    <input type="checkbox" name="toppings[]" value="cheese">

    Now, notice that all three of my checkboxes are named the same, with the added "[]"

    That [] tells PHP that this is an "array" of
    values.

    In short, you typically want to treat checkbox values as an array, since more
    than one checkbox can be selected in a form.

    The same can be said of the radio button.

    The key difference between radio's and checkboxes' is that only one radio of a given name may be selected in a given form.

    So, when you submit your "hamburger" order, you simply loop through the toppings array to
    get the "separate" toppings.

    for example:

    for ($i=0; $i < count($toppings); $i++) {
    echo $toppings[$i]."<br>";
    }

    To play with this here's a more complete script:

    <html>
    <head>
    <title>Hamburger</title>
    </head>
    <body bgcolor="#ffffff">
    <?php
    if ($submit) {
    echo "You Ordered a Hamburger with:<br>";
    echo "<ul>";
    for ($i=0; $i<count($toppings); $i++) {
    echo "<li>".$toppings[$i]."</li>";
    }
    echo "</ul>";
    }
    else {
    ?>
    <form method="post" action="<?php echo $PHP_SELF; ?>">
    Select the toppings you want on your Hamburger.<br>
    Pickles: <input type="checkbox" name="toppings[]" value="pickles"><br>
    Onions: <input type="checkbox" name="toppings[]" value="onions"><br>
    Cheese: <input type="checkbox" name="toppings[]" value="cheese"><br>
    Ketchup: <input type="checkbox" name="toppings[]" value="ketchup"><br>
    Mustard: <input type="checkbox" name="toppings[]" value="mustard"><br>
    <input type="submit" name="submit" value="Order It!">
    <?php
    }
    ?>
    </form>
    </body>

    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    4
    Rep Power
    0
    Thank you Billyo, Seti and Robert_J_Sherman. Your help is much appreciated.

IMN logo majestic logo threadwatch logo seochat tools logo