Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Submitting a Form Web developers frequently use PHP to process forms. XHTML form

ID: 2246821 • Letter: S

Question

Submitting a Form

Web developers frequently use PHP to process forms. XHTML forms offer two different methods for communicating form values to a web server: get and post. The server handles both methods similarly by placing the input values into elements of an implicitly declared array, $_GET and $_POST, respectively. In your PHP code, to access a value, use the name of the input field as the subscript. For example, the value of the name form field is either $_GET["name"] or $_POST["name"], depending on the submission method.

The get and post methods differ on the client side. In the get method, the browser appends the field names and values to the URL, like this:

http://localhost/unit6/toys.php?Name=Mystery+Shopper&Age=99&Item=2

The post method transmits the field names and values as part of the XHTML header data and so the user cannot easily see or modify this information.

Open your catalog page from unit 4 in a text editor. Update the form tag to include method="post" or method="get". In the action attribute, specify the name of a page that will process the submitted form. Finally, write content for the page you specified. In this case, simply confirm receipt of the order, the date, and all of the field values.

Submit your pages to the W3C validator (http://validator.w3.org/) and correct all errors. (Note: You may ignore an error concerning placement of the script element.) Include screenshots of the results in your submission. Also include the full XHTML code for each page, and screenshots of the pages as your web browser displays them. Summarize the steps you followed to create and perfect the website. Outline the difficulties you encountered and how you resolved them.

When I open the memorabilia.php in a browser, it looks fine. However, when I enter data and hit submit, the memorabilia3.php code displays instead of running it and producing a receipt. Below is code for memorabilia.php and memorabilia3.php.

--------------------------------------------------------------------------------------------------------

Memorabilia.php

<?php echo '<?xml version="1.0" encoding="IUTF-8"?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset = "utf-8" />
<title> Bears and Butterflies Catalog Page</title>
<link rel="stylesheet" type= "text/css" href="style.css">
</head>
<body>
<img src="images/bearsboard.jpg" alt = "Bears Board" /> <br />
<h3><a href="index.htm">Home Page</a></h3>


<form action = "memorabilia3.php" method = "post">
<h2>Chicago Bears Memorabilia</h2>

<tr>
<td> Buyer's Name: </td>
<td> <input type = "text" name = "name"
size = "30" />
</td>

<tr>
<td> Street Address: </td>
<td> <input type = "text" name = "name"
size = "30" />
</td>

<tr>
<td> City, State & Zip: </td>
<td> <input type = "text" name = "name"
size = "30" /> </br >
</td>
</tr>
</table>
<p/>

<table border = "border">

<tr>
<th> Select </th>
<th> Item </th>
<th> Description </th>
<th> Photo </th>
<th> Unit Price </th>
<th> Quantity </th>
</tr>
<tr>

<td><label><input type= "radio" name = "pickone" value = "1"
id = "pick" /> </label></td>
<th>Mini Helmet</th>
<td>Brian Urlacher Signed Mini Helmet</td>
<td><img src="images/SignedUrlacherminihelmet.jpg" alt = "Mini Urlacher helmet" /></td>
<td>$169.99</td>
<td> <input type = "text" name = "Mini Helmet" size = "2" /> </td>

</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "1"
/> </label></td>
<th>Pro Line Helmet</th>
<td>Chicago Bears Butkus/Singletary/Urlacher Monsters Of The Midway Signed Limited Edition Pro Line Helmet</td>
<td><img src="images/butkussingletaryurlacher.jpg" alt = "Monsters of the Midway Pro Line Helmet" /></td>
<td>$499.99</td>
<td> <input type = "text" name = "Pro Line Helmet" size = "2" /> </td>
</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "2"
/> </label></td>
<th>Portrait</th>
<td>Walter Payton Chicago Bears Deluxe Framed Collectible With Autographed 1991 Proline Portraits Card</td>
<td><img src="images/WalterPaytonHOF.jpg" alt = "Framed Walton Payton HOF Proline Card" /></td>
<td>$349.99</td>
<td> <input type = "text" name = "Portrait" size = "2" /> </td>
</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "3"
/> </label></td>
<th>Photograph</th>
<td>Framed Mike Ditka Chicago Bears Autographed 8'' X 10'' Finger Shot Blue Ink Photograph With "You're #1" Inscription</td>
<td><img src="images/mikeditkaframedpic.jpg" alt = "Framed & Autographed Mike Ditka Pic" /></td>
<td>$99.99</td>
<td> <input type = "text" name = "Photograph" size = "2" /> </td>
</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "4"
/> </label></td>
<th>Football</th>
<td>Autographed Walter Payton Football - #34 JSA Certified</td>
<td><img src="images/walterpaytonfootball.jpg" alt = "Autographed Walter Payton Football" /></td>
<td>$1629.99</td>
<td> <input type = "text" name = "Football" size = "2" /> </td>
</tr>

</table>
<p/>

<!-- The radio buttons for the payment method -->
<h3> Payment Method </h3>
<p>
<input type = "radio" name = "payment" value = "visa"
checked = "checked" />
Visa <br />
<input type = "radio" name = "payment" value = "mc" />
Master Card <br />
<input type = "radio" name = "payment" value = "discover" />
Discover <br />
<input type = "radio" name = "payment" value = "check" />
Check <br /> <br />


<!-- The submit and reset buttons -->
<input type = "submit" value = "Submit Form" />
<input type = "reset" value = "Reset Form" />
</p>
</form>
</body>
</html>

----------------------------------------------------------------------------------------

memorabilia3.php

<?php echo'<xml version="1.0" encoding="IUTF-8"?>';?>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title> Process the memorabilia.php form</title>
</head>
<body>
<?php

//Get form data values

$minihelmet = $_POST["minihelmet"];
$prolinehelmet = $_POST["prolinehelmet"];
$portrait = $_POST["portrait"];
$photograph = $_POST["photograph"];
$football = $_POST["football"];
$name = $_POST["name"];
$street = $_POST["street"];
$city = $_POST["city"];
$payment = $_POST["payment"];

//If any of the quantities are blank, set them to zero

if ($minihelmet == "") $minihelmet = 0;
if ($prolinehelmet == "") $prolinehelmet = 0;
if ($portrait == "") $portrait = 0;
if ($photograph == "") $photograph = 0;
if ($football == "") $football=0;

//Compute the item costs and total cost

$minihelmet_cost = 169.99 * $minihelmet;
$prolinehelmet_cost = 499.99 * $prolinehelmet;
$portrait_cost = 349.99 * $portrait;
$photograph_cost = 99.99 * $photograph;
$football_cost = 1629.99 * $football;
$total_price =$minihelmet_cost + $prolinehelmet_cost +
$portrait_cost + $photograph_cost + $football_cost;

$total_items =$minihelmet + $prolinehelmet +
$portrait + $photograph + $football;

//Return the results to the browser in a table

?>
<h4> Customer:</h4>
<?php
print ("$name<br/>$street<br/>$city<br/>");
?>
<p/><p/>

<table border = "border">
<caption> Order Information </caption>
<tr>
<th> Product </th>
<th> Unit Price </th>
<th> Quantity Ordered </th>
<th> Item Cost </th>
</tr>
<tr align = "center">
<td> Mini Helmet </td>
<td> $169.99 </td>
<td> <?php print ("$minihelmet"); ?> </td>
<td> <?php printf ("$ %4.2f", $minihelmet_cost); ?>
</td>
</tr>
<tr align = "center">
-5-
<td> Pro Line Helmet </td>
<td> $499.99 </td>
<td> <?php print ("$prolinehelmet"); ?> </td>
<td> <?php printf ("$ %4.2f", $prolinehelmet_cost); ?>
</td>
</tr>
<tr align = "center">
<td> Portrait </td>
<td> $4.50 </td>
<td> <?php print ("$portrait"); ?> </td>
<td> <?php printf ("$ %4.2f", $portrait_cost); ?>
</td>
</tr>
<tr align = "center">
<td> Photograph </td>
<td> $5.00 </td>
<td> <?php print ("$photograph"); ?> </td>
<td> <?php printf ("$ %4.2f", $photograph_cost); ?>
</td>
</tr>
<tr align = "center">
<td> Football </td>
<td> $5.00 </td>
<td> <?php print ("$football"); ?> </td>
<td> <?php printf ("$ %4.2f", $football_cost); ?>
</td>
</tr>
</table>
<p /> <p />
<?php
print ("You ordered $total_items Memorabilia items <br />");
printf ("Your total bill is: $ %5.2f <br />", $total_price);
print ("Your chosen method of payment is: $payment <br />");
?>
</body>
</html>

Explanation / Answer

Memorabilia.php

<?php echo '<?xml version="1.0" encoding="IUTF-8"?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset = "utf-8" />
<title> Bears and Butterflies Catalog Page</title>
<link rel="stylesheet" type= "text/css" href="style.css">
</head>
<body>
<img src="images/bearsboard.jpg" alt = "Bears Board" /> <br />
<h3><a href="index.htm">Home Page</a></h3>


<form action = "memorabilia3.php" method = "post">
<h2>Chicago Bears Memorabilia</h2>

<tr>
<td> Buyer's Name: </td>
<td> <input type = "text" name = "name"
size = "30" />
</td>

<tr>
<td> Street Address: </td>
<td> <input type = "text" name = "name"
size = "30" />
</td>

<tr>
<td> City, State & Zip: </td>
<td> <input type = "text" name = "name"
size = "30" /> </br >
</td>
</tr>
</table>
<p/>

<table border = "border">

<tr>
<th> Select </th>
<th> Item </th>
<th> Description </th>
<th> Photo </th>
<th> Unit Price </th>
<th> Quantity </th>
</tr>
<tr>

<td><label><input type= "radio" name = "pickone" value = "1"
id = "pick" /> </label></td>
<th>Mini Helmet</th>
<td>Brian Urlacher Signed Mini Helmet</td>
<td><img src="images/SignedUrlacherminihelmet.jpg" alt = "Mini Urlacher helmet" /></td>
<td>$169.99</td>
<td> <input type = "text" name = "Mini Helmet" size = "2" /> </td>

</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "1"
/> </label></td>
<th>Pro Line Helmet</th>
<td>Chicago Bears Butkus/Singletary/Urlacher Monsters Of The Midway Signed Limited Edition Pro Line Helmet</td>
<td><img src="images/butkussingletaryurlacher.jpg" alt = "Monsters of the Midway Pro Line Helmet" /></td>
<td>$499.99</td>
<td> <input type = "text" name = "Pro Line Helmet" size = "2" /> </td>
</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "2"
/> </label></td>
<th>Portrait</th>
<td>Walter Payton Chicago Bears Deluxe Framed Collectible With Autographed 1991 Proline Portraits Card</td>
<td><img src="images/WalterPaytonHOF.jpg" alt = "Framed Walton Payton HOF Proline Card" /></td>
<td>$349.99</td>
<td> <input type = "text" name = "Portrait" size = "2" /> </td>
</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "3"
/> </label></td>
<th>Photograph</th>
<td>Framed Mike Ditka Chicago Bears Autographed 8'' X 10'' Finger Shot Blue Ink Photograph With "You're #1" Inscription</td>
<td><img src="images/mikeditkaframedpic.jpg" alt = "Framed & Autographed Mike Ditka Pic" /></td>
<td>$99.99</td>
<td> <input type = "text" name = "Photograph" size = "2" /> </td>
</tr>

<tr>
<td><label><input type= "radio" name = "pickone" value = "4"
/> </label></td>
<th>Football</th>
<td>Autographed Walter Payton Football - #34 JSA Certified</td>
<td><img src="images/walterpaytonfootball.jpg" alt = "Autographed Walter Payton Football" /></td>
<td>$1629.99</td>
<td> <input type = "text" name = "Football" size = "2" /> </td>
</tr>

</table>
<p/>

<!-- The radio buttons for the payment method -->
<h3> Payment Method </h3>
<p>
<input type = "radio" name = "payment" value = "visa"
checked = "checked" />
Visa <br />
<input type = "radio" name = "payment" value = "mc" />
Master Card <br />
<input type = "radio" name = "payment" value = "discover" />
Discover <br />
<input type = "radio" name = "payment" value = "check" />
Check <br /> <br />


<!-- The submit and reset buttons -->
<input type = "submit" value = "Submit Form" />
<input type = "reset" value = "Reset Form" />
</p>
</form>
</body>
</html>

----------------------------------------------------------------------------------------

memorabilia3.php

<?php echo'<xml version="1.0" encoding="IUTF-8"?>';?>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title> Process the memorabilia.php form</title>
</head>
<body>
<?php

//Get form data values

$minihelmet = $_POST["minihelmet"];
$prolinehelmet = $_POST["prolinehelmet"];
$portrait = $_POST["portrait"];
$photograph = $_POST["photograph"];
$football = $_POST["football"];
$name = $_POST["name"];
$street = $_POST["street"];
$city = $_POST["city"];
$payment = $_POST["payment"];

//If any of the quantities are blank, set them to zero

if ($minihelmet == "") $minihelmet = 0;
if ($prolinehelmet == "") $prolinehelmet = 0;
if ($portrait == "") $portrait = 0;
if ($photograph == "") $photograph = 0;
if ($football == "") $football=0;

//Compute the item costs and total cost

$minihelmet_cost = 169.99 * $minihelmet;
$prolinehelmet_cost = 499.99 * $prolinehelmet;
$portrait_cost = 349.99 * $portrait;
$photograph_cost = 99.99 * $photograph;
$football_cost = 1629.99 * $football;
$total_price =$minihelmet_cost + $prolinehelmet_cost +
$portrait_cost + $photograph_cost + $football_cost;

$total_items =$minihelmet + $prolinehelmet +
$portrait + $photograph + $football;

//Return the results to the browser in a table

?>
<h4> Customer:</h4>
<?php
print ("$name<br/>$street<br/>$city<br/>");
?>
<p/><p/>

<table border = "border">
<caption> Order Information </caption>
<tr>
<th> Product </th>
<th> Unit Price </th>
<th> Quantity Ordered </th>
<th> Item Cost </th>
</tr>
<tr align = "center">
<td> Mini Helmet </td>
<td> $169.99 </td>
<td> <?php print ("$minihelmet"); ?> </td>
<td> <?php printf ("$ %4.2f", $minihelmet_cost); ?>
</td>
</tr>
<tr align = "center">
-5-
<td> Pro Line Helmet </td>
<td> $499.99 </td>
<td> <?php print ("$prolinehelmet"); ?> </td>
<td> <?php printf ("$ %4.2f", $prolinehelmet_cost); ?>
</td>
</tr>
<tr align = "center">
<td> Portrait </td>
<td> $4.50 </td>
<td> <?php print ("$portrait"); ?> </td>
<td> <?php printf ("$ %4.2f", $portrait_cost); ?>
</td>
</tr>
<tr align = "center">
<td> Photograph </td>
<td> $5.00 </td>
<td> <?php print ("$photograph"); ?> </td>
<td> <?php printf ("$ %4.2f", $photograph_cost); ?>
</td>
</tr>
<tr align = "center">
<td> Football </td>
<td> $5.00 </td>
<td> <?php print ("$football"); ?> </td>
<td> <?php printf ("$ %4.2f", $football_cost); ?>
</td>
</tr>
</table>
<p /> <p />
<?php
print ("You ordered $total_items Memorabilia items <br />");
printf ("Your total bill is: $ %5.2f <br />", $total_price);
print ("Your chosen method of payment is: $payment <br />");
?>
</body>
</html>