SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    0 Post(s)
    0 Thread(s)

    Cannot get variables to work..


    I am brand new to PHP as of this semester of college. I have been breezing through a few labs and now I am stumped. Here is the proposed script to write:

    Compose a script that creates a form that presents a customer a menu of choices for ice cream sundaes. Show vanilla, chocolate and peanut butter as ice cream flavors.

    Display cherries, chocolate sprinkles, pineapple and walnuts as toppings. Allow neither the choice of chocolate sprinkles with chocolate ice cream, nor walnuts with peanut butter ice cream. Print a warning to the customer that those choices are not compatible.

    Print to the monitor a final sundae build with flavor and toppings.

    So, as of now, I have radio buttons set for all of the options. Two separate forms for both. My problem is getting a message to show up when certain combinations are selected. I cannot get the variables to work. I get an 'undefined index' when loading the page.

    Here is what I have; I am trying an if/else statement when certain options are displayed.



    <form action = "final.php" method="post">
    <center><h1>Crazy Ice Cream Lab 4</center></h1>
    <div id="icecream">
    <label>What kind of Ice Cream do you want?</label>
    <br />
    <input type="radio" name="icecream" value="vanilla"> Vanilla <br />
    <input type="radio" name="icecream" value="chocolate"> Chocolate <br />
    <input type="radio" name="icecream" value="peanutbutter"> Peanut Butter <br />

    <form action = "final.php" method="post">
    <div id="toppings">
    <br />
    <label>what toppings would you like? </label>
    <br />
    <input type="radio" name="toppings" value="cherries"> Cherries <br />
    <input type="radio" name="toppings" value="chocolatesprinkles"> Chocolate Sprinkles <br />
    <input type="radio" name="toppings" value="pineapple"> Pineapple <br />
    <input type="radio" name="toppings" value="walnuts"> Walnuts <br />

    <div id="buttons">
    <input type="submit" value="Submit"><br />

    $icecream = $_POST['icecream'];
    $toppings = $_POST['toppings'];

    if ($icecream == "vanilla" and $toppings == "cherries") {
    echo "this is not a valid choice.";
    echo "";



    Also, would it be possible to disable the submit button also with certain choices selected? I have tried looking up everything, nothing has been much help. Thanks for any insight you guys might have.

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Michigan, USA
    2 Post(s)
    0 Thread(s)
    You get "index undefined" because when you first load the page there is nothing in $_POST. You may want to look up isset()
    - Robert

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    France, deep rural.
    17 Post(s)
    1 Thread(s)
    You have made 2 forms when you don't really need to.

    You have compounded the problem by making the first form without a submit button, so that data would never get sent.

    Create a single <form></form> element, then in final.php do var_dump($_POST) to assess what is being sent to the form handler by the form.

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    7 Post(s)
    0 Thread(s)
    You may also wish to change to && instead of "and". So your IF statement might look like

    PHP Code:
    if (isset($icecream) && $icecream == "vanilla" && isset($toppings) && $toppings == "cherries") { 

Tags for this Thread


Posting Permissions

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