...

View Full Version : Help with PHP Pizza Topping Selector exercise



fullmontez
05-04-2009, 09:11 PM
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 (http://books.google.com/books?id=If3TLnM0s3kC&pg=RA1-PA97&lpg=RA1-PA97&dq=php+pizza+toppings+selector&source=bl&ots=z0ewP-6NV0&sig=Eqy-YcrY0azGkTVtxdfjIZ8eQug&hl=en&ei=1zn_SZHOOojMM5n7uM0E&sa=X&oi=book_result&ct=result&resnum=3#PPP1,M1) 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


<!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



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

fullmontez
05-04-2009, 10:25 PM
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?

Len Whistler
05-04-2009, 10:41 PM
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.


----------

fullmontez
05-04-2009, 10:51 PM
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).

Fou-Lu
05-04-2009, 10:53 PM
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.

Len Whistler
05-04-2009, 10:55 PM
The php code you have appears to be correct, and should work. I don't understand the carriage return though.


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

fullmontez
05-04-2009, 11:01 PM
No probelm Fou-Lou. The source code for pizza.php as it is generated after submitting the form from pizza.html is as follows:


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

Fou-Lu
05-04-2009, 11:05 PM
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.

Len Whistler
05-04-2009, 11:08 PM
fullmontez ..... I ran that on my desktop and get the same result. Is XAMPP running?



-------

fullmontez
05-04-2009, 11:16 PM
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
...
?>
and ran as intended. I'm going to do a moron step now and just restart my computer.

fullmontez
05-04-2009, 11:36 PM
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.

Fumigator
05-04-2009, 11:38 PM
Does your file have a file extension of HTML or PHP? It needs to be PHP.

CalumK
05-04-2009, 11:45 PM
try this...


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

fullmontez
05-05-2009, 05:23 AM
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.

Fumigator
05-05-2009, 04:49 PM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum