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 15 of 15
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Help with PHP Pizza Topping Selector exercise

    Hello all,

    I have a good grasp of HTML/CSS (easy things, for sure) and am trying to learn more about PHP. I have been using a book PHP: A Beginner's Guide by Vikram Vaswani with mostly good results.

    However, In Chapter 4: Working with Arrays, I cannot get a particular exercise to work for me at all and need some help.

    The example starts with a form html file: pizza.html

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    	"DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title>Project 4-2: Pizza Topping Selector</title>
    </head>
    
    
    <body>
      <h2>Project 4-2: Pizza Topping Selector</h2>
      
      <form method="post" action="pizza.php">
        <p>Select your favorite pizza toppings: <br /></p>
    
        <input type="checkbox" name="toppings[]" value="tomato">Tomato</input>
    	<input type="checkbox" name="toppings[]" value="onion">Onion</input>
    	<input type="checkbox" name="toppings[]" value="jalapenos">Jalapeno Peppers</input>
    	<input type="checkbox" name="toppings[]" value="olives">Olives</input>
    	<input type="checkbox" name="toppings[]" value="mint">Mint</input>
    	<input type="checkbox" name="toppings[]" value="pineapple">Pineapple</input>
    	<input type="checkbox" name="toppings[]" value="bacon">Bacon</input>
    	<input type="checkbox" name="toppings[]" value="chicken">Chicken</input>
    	<input type="checkbox" name="toppings[]" value="ham">Ham</input>
    	<input type="checkbox" name="toppings[]" value="anchovies">Anchovies</input>
    	<input type="checkbox" name="toppings[]" value="x-cheese">Extra Cheese</input>
    	
    	<p />
    	
    	<input type="submit" name="submit" value="Submit" />
      </form>  
    
    </body>
    </html>
    Once the form is submitted, it access the PHP file: pizza.php

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    	"DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title>Project 4-2: Pizza Topping Selector</title>
    </head>
    
    
    <body>
      <h2>Project 4-2: Pizza Topping Selector</h2>
      
      You selected the following toppings: <br />
      <ul>
      
    <?php
        foreach ($_POST['toppings'] as $t) {
    	  echo "<li>$t</li> \r\n";
    	}
      ?>
    
      </ul>
      
    </body>
    </html>
    The output is supposed to give you an unordered list of the toppings you selected in the HTML form. However, what I'm getting is a portion of my PHP source code:

    Project 4-2: Pizza Topping Selector
    You selected the following toppings:

    $t \r\n"; } ?>
    I have tried replacing the double quotes " with single quotes ' and I've checked & rechecked for open tags or quotes and cannot find the problem. The script runs and does not give me an error either. I'm curious as to whether or not the array is being populated by the submission of the form or if the problem is unrelated to the array.

    Any help would be much appreciated. Thank you.

  • #2
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Upon further review and other attempted exercises in this chapter I've noticed that my PHP keeps breaking following a greater than sign ">". In other exercises, pieces of the PHP source code will begin to appear on the screen next to the Submit button.

    Each time this occurs, it begins directly after > and continues to the end of the PHP code.

    I am using Notepad++ to code and XAMPP to test locally on my PC.

    Does anyone know why this occurs?

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Make sure pizza.php is saved as a php file, not a html file. And it's in the proper folder for XAMPP, not on the desktop.


    ----------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    fullmontez (05-08-2009)

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you for the reply Len.

    This exercise involves two files. The pizza.html file that is used to submit the form and the pizza.php file that has the short script used to list the toppings. I have ensured that each are labeled as they are supposed to be.

    I am convinced that the problem lies with the greater than sign (>) for some reason as I've had problems with other exercises involving the same outcome (ie: printing out php source code onto the page).

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Can you post what you're actual source code shows please?

    I should be more specific. I want to see what you're html source code is when run through you're browser on pizza.php. Make sure its posted from the form though, it may trigger a warning otherwise.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    fullmontez (05-08-2009)

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    The php code you have appears to be correct, and should work. I don't understand the carriage return though.

    PHP Code:
    <?php
        
    foreach ($_POST['toppings'] as $t) {
          echo 
    "<li>$t</li> \r\n";
        }
    ?>
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    fullmontez (05-08-2009)

  • #7
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts
    No probelm Fou-Lou. The source code for pizza.php as it is generated after submitting the form from pizza.html is as follows:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    	"DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title>Project 4-2: Pizza Topping Selector</title>
    </head>
    
    
    <body>
      <h2>Project 4-2: Pizza Topping Selector</h2>
      
      You selected the following toppings: <br />
    
      <ul>
      
      <?php
        foreach ($_POST['toppings'] as $t) {
    	  echo "<li>$t</li> \r\n";
    	}
      ?>
      
      </ul>
      
    </body>
    </html>
    I tried to highlight what the source is showing me. It shows from the beginning of the PHP code until the > of <li> is italicized and in another color. The rest of PHP code after that is what is being displayed on screen.

    Len, the carriage return is only there because it was in the book. I also thought that it was unnecessary, but was erring on the side of caution.

    Thank you both.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What are you using to interpret this code as PHP? Apache?
    The problem is that its configured to be served as text/html without PHP preprocessing. That is shown in you're source, the <?php... is literally being identified as a tag.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    fullmontez (05-08-2009)

  • #9
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    fullmontez ..... I ran that on my desktop and get the same result. Is XAMPP running?



    -------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    fullmontez (05-08-2009)

  • #10
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you again Fou-Lou. I was thinking something along those lines, but I'm not sure how to repair this issue. XAMPP is running Apache, MySQL, and Filezilla. For this exercise I believe I'm only using Apache.

    Yes, Len. I am using XAMPP and it is running in my toolbar and the XAMPP control panel indicates that everything is operating as it should.

    I had previously worked with this exact setup for simpler PHP programs, and it recognized
    PHP Code:
    <?php
    ...
    ?>
    and ran as intended. I'm going to do a moron step now and just restart my computer.

  • #11
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts
    After a computer restart and some more investigation, I've come to the conclusion that it must be XAMPP. Previous PHP files that I'd written and had operated correctly are now starting to show the same issues when encountering the first greater than sign > after the <?php statement.

    Will search for a way to fix this. Thank you Fou & Len.

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Does your file have a file extension of HTML or PHP? It needs to be PHP.

  • #13
    New Coder
    Join Date
    Jan 2009
    Posts
    58
    Thanks
    4
    Thanked 3 Times in 3 Posts
    try this...

    PHP Code:
    <?php
        
    foreach ($_POST['toppings'] as $t) {
          echo 
    "<li>".$t."</li> \r\n";
        }
    ?>

  • #14
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you everyone for the advice and help. The problem was indeed XAMPP. I'm not sure exactly what happened, but even after a fresh install of the program, none of my PHP scripts would work.

    I installed XAMPP Lite on a flash drive and now all of my scripts are working again. I'm going to uninstall XAMPP and deep scrub my HDD for all XAMPP files to hopefully get it working again on the PC itself. However, I'm liking the idea of XAMPP Lite on the thumbdrive for mobile purposes.

    Once again, thank you, and I hope to be a helpful part of this community in the future.

  • #15
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yeah I'm using Xampp Lite on my flash drive right now but I'm finding some things to be too slow for everyday development (primarily myPhpAdmin is reaallly slow).

  • Users who have thanked Fumigator for this post:

    fullmontez (05-08-2009)


  •  

    Posting Permissions

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