fullmontez
05-04-2009, 08: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.
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.