Introduction to PHP.

Introduction to PHP.

Form Handling.

Form handling in PHP is very simple







The Popcorn Sales Example




<!-- popcorn.html - This describes popcorn sales form page -->
<html>
<head>
<title> Popcorn Sales CGI program </title>
</head>
<body>

<!-- The next line gives the address of the CGI program -->

<form action = "popcorn3.php"
      method = "POST">
<h2> Welcome to Millennium Gymnastics Booster Club Popcorn Sales </h2>

<table>

<!-- Text widgets for name and address -->

    <tr>
        <td> Buyer's Name: </td>  
        <td> <input type = "text"  name = "name"  size = "30"> </td>
    </tr>
    <tr>
        <td> Street Address: </td>
        <td> <input type = "text"  name = "street"  size = "30"> </td>
    </tr>
    <tr>
        <td> City, State, Zip: </td>
        <td> <input type = "text"  name = "city"  size = "30"> </td>
    </tr>
</table>
<br/>

<table border = "border">

<!-- First, the column headings --> 

    <tr>
        <th> Product Name </th>
        <th> Price </th>
        <th> Quantity </th>
    </tr>

<!-- Now, the table data entries -->

    <tr>
        <td> Unpopped Popcorn (1 lb.) </td>
        <td> $3.00 </td>
        <td> <input type = "text"  name = "unpop"  size ="2"> </td>
    </tr>
    <tr>
        <td> Caramel Popcorn (2 lb. canister) </td>
        <td> $3.50 </td>
        <td> <input type = "text"  name = "caramel"  
              size = "2"> </td>
    </tr> 
    <tr>
        <td> Caramel Nut Popcorn (2 lb. canister) </td>
        <td> $4.50 </td>
        <td> <input type = "text"  name = "caramelnut"  
              size = "2"></td>
    </tr>
    </tr>
        <td> Toffey Nut Popcorn (2 lb. canister) </td>
        <td> $5.00 </td>
        <td> <input type = "text"  name = "toffeynut"  
                      size = "2"> </td>
    </tr> 

</table>
<br/>

<!-- The radio buttons for the payment method -->

<h3> Payment Method: </h3>
<input type = "radio"  name = "payment"  value = "visa" 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 Order">
<input type = "reset"  value = "Clear Order Form">

</form>
</body>
</html>

The script that handles the popcorn form is given below.



<(!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//EN"
"http://www.w3.org/TR/xhtml1/dtd/xhtml1-strict.dtd">

<!--	popcorn3.php
	Processes the data collected from popcorn3.html
	-->

<html>
<head>
<title>  
Procees the Propcorn order form
</title>
</head>
<body> 

<?php
// If any of the quatities are blank, set them to zero

if ($unpop == "") $unpop = 0;
if ($caramel == "") $caramel = 0;
if ($caramelnut == "" ) $caramelnut = 0;
if ($toffeynut == "") $toffeynut =0;

//Compute the item costs and total cost

$unpop_cost = $unpop * 3.0;
$caramel_cost =  $caramel *3.5;
$caramelnut_cost = 4.5 * $caramelnut ;
$toffeynut_cost = 5.0 * $toffeynut;

$total_price = $unpop_cost + $caramel_cost +
	$caramelnut_cost + $toffeynut_cost ;

$total_items = $unpop + $caramel + $caramelnut + $toffeynut;

//Return results to the browser in a table

?>

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

<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 aling = "center">
		<td> Unpopped Popcorn (1 lb.) </td>
        	<td> $3.00 </td>
        	<td> <?php print ("$unpop");?> </td>
		<td> <?php printf ("$ %4.2f", $unpop_cost); ?> </td>
	</tr>
	<tr aling = "center">
        	<td> Caramel Popcorn (2 lb. canister) </td>
        	<td> $3.50 </td>
        	<td> <?php print ("$caramel");?> </td>
                <td> <?php printf ("$ %4.2f", $caramel_cost); ?> </td>
        </tr>   
	<tr aling = "center">
        	<td> Caramel Nut Popcorn (2 lb. canister) </td>
        	<td> $4.50 </td>
        	<td> <?php print ("$caramelnut");?> </td>
                <td> <?php printf ("$ %4.2f", $caramelnut_cost); ?> </td>
        </tr>   

	
	<tr aling = "center">
        	<td> Toffey Nut Popcorn (2 lb. canister) </td>
        	<td> $5.00 </td>
        	<td> <?php print ("$toffeynut");?> </td>
                <td> <?php printf ("$ %4.2f", $toffeynut_cost); ?> </td>
        </tr>        
</table>
<br /><br />

<?php
print ("You ordered $total_items popcorn items <br/> <br/> \n");
printf ("Your total bill is: \$ %5.2f <br />", $total_price);
print ("Your chosen method of payment is: $payment <br />");
?>
</body>
</html>

The Popcorn Sales order form




| Origins and Uses of PHP | | Overview of PHP | | General Syntactic Characteristics |
| Primitives, Operations, and Expressions | | Output | | Control Statements |
| Arrays | | Functions | | Pattern Matching |
| Form Handling | | Files | | Cookies | | Session Tracking |