User Tools

Site Tools


eg-259:lecture18

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
eg-259:lecture18 [2009/11/24 14:50] eechriseg-259:lecture18 [2013/03/08 18:04] (current) – [PHP for Web Applications] eechris
Line 2: Line 2:
 ====== PHP for Web Applications ====== ====== PHP for Web Applications ======
  
-**Lecture 18**: To be given on Monday 30th November, 2009.+**Supplementary Material** 
 + 
 +Provided for Reference. This material is no longer taught on this module.
  
 **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]]. **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]].
  
 Using PHP for web applications development. Using PHP for web applications development.
- 
  
  
Line 227: Line 228:
  
     * If the query string has ''colour=light&taste=malty''     * If the query string has ''colour=light&taste=malty''
-    * ''$_POST["colour")'' will return ''"light"'' and ''$_POST["taste")'' will return ''"malty"'' +    * ''$_POST["colour"]'' will return ''"light"'' and ''$_POST["taste"]'' will return ''"malty"'' 
     * Query matching will also turn check box group ''colour=red&colour=blue'' to an array so that ''$_POST["colour"]'' returns the right thing.      * Query matching will also turn check box group ''colour=red&colour=blue'' to an array so that ''$_POST["colour"]'' returns the right thing. 
  
Line 243: Line 244:
    * //The Form Page (just HTML)// : [[/~eechris/eg-259/examples/lecture18/popcorn3.html|popcorn3.html]] ( [[http://localhost/eg-259/examples/lecture18/popcorn3.html|popcorn3.html @ localhost]] )    * //The Form Page (just HTML)// : [[/~eechris/eg-259/examples/lecture18/popcorn3.html|popcorn3.html]] ( [[http://localhost/eg-259/examples/lecture18/popcorn3.html|popcorn3.html @ localhost]] )
  
-{{eg-259:l18-popcorn3.html.png|The Form Page }}+<html> 
 +    <form action = "popcorn3.php" 
 +    method = "post"> 
 +      <h2> Welcome to Millennium Gymnastics Booster Club Popcorn 
 +      Sales </h2> 
 +      <table> 
 +        <!-- Text widgets for the customer's name and address --
 +        <tr> 
 +          <td> Buyer's Name</td> 
 +          <td> 
 +          <input type = "text" name = "name" 
 +          size = "30" required placeholder="Your name"/> 
 +          </td> 
 +        </tr> 
 +        <tr> 
 +          <td> Street Address: </td> 
 +          <td> 
 +          <input type = "text" name = "street" 
 +          size = "30" required placeholder = "Street"/> 
 +          </td> 
 +        </tr> 
 +        <tr> 
 +          <td> City, State, Zip: </td> 
 +          <td> 
 +          <input type = "text" name = "city" 
 +          size = "30" required placeholder="City, State, Zip"/> 
 +          </td> 
 +        </tr> 
 +      </table> 
 +      <p /> 
 +      <table border = "border"> 
 +        <!-- First, the column headings --> 
 +        <tr> 
 +          <th> Product </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 align = "center"> 
 +          <input type = "number" name = "unpop" 
 +          size = "3" min="0" pattern="\d+" /> 
 +          </td> 
 +        </tr> 
 +        <tr> 
 +          <td> Caramel Popcorn (2 lb. canister) </td> 
 +          <td> $3.50 </td> 
 +          <td align = "center"> 
 +          <input type = "number" name = "caramel" 
 +          size = "3" min="0" pattern="\d+" /> 
 +          </td> 
 +        </tr> 
 +        <tr> 
 +          <td> Caramel Nut Popcorn (2 lb. canister) </td> 
 +          <td> $4.50 </td> 
 +          <td align = "center"> 
 +          <input type = "number" name = "caramelnut" 
 +          size = "3" min="0" pattern="\d+" /> 
 +          </td> 
 +        </tr> 
 +        <tr> 
 +          <td> Toffey Nut Popcorn (2 lb. canister) </td> 
 +          <td> $5.00 </td> 
 +          <td align = "center"> 
 +          <input type = "number" name = "toffeynut" 
 +          size = "3" min="0" pattern="\d" /> 
 +          </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 Order" /> 
 +        <input type = "reset" value = "Clear Order Form" /> 
 +      </p> 
 +    </form> 
 +</html>
  
 ---- ----
  
 <code html> <code html>
-<?xml version = "1.0" encoding = "utf-8"?> +<!DOCTYPE html>
-<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" +
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +
- +
 <!-- popcorn3.html - This describes the popcorn sales form --> <!-- popcorn3.html - This describes the popcorn sales form -->
-<html xmlns = "http:///www.w3.org/1999/xhtml">+<html lang="en">
   <head>   <head>
 +    <meta charset="utf-8" />
     <title> Popcorn Sales - for PHP handling </title>     <title> Popcorn Sales - for PHP handling </title>
   </head>   </head>
   <body>   <body>
- +    <form action = "popcorn3.php" 
-    <form action = "popcorn3.php"  +    method = "post">
-          method = "post">+
       <h2> Welcome to Millennium Gymnastics Booster Club Popcorn       <h2> Welcome to Millennium Gymnastics Booster Club Popcorn
-           Sales </h2> +      Sales </h2>
       <table>       <table>
- +        <!-- Text widgets for the customer's name and address -->
-<!-- Text widgets for the customer's name and address --> +
         <tr>         <tr>
           <td> Buyer's Name: </td>           <td> Buyer's Name: </td>
-          <td> <input type = "text" name = "name"  +          <td> 
-                      size = "30" /></td>+          <input type = "text" name = "name" 
 +          size = "30" required placeholder="Your name"/> 
 +          </td>
         </tr>         </tr>
         <tr>         <tr>
           <td> Street Address: </td>           <td> Street Address: </td>
-          <td> <input type = "text" name = "street"  +          <td> 
-                      size = "30" /></td>+          <input type = "text" name = "street" 
 +          size = "30" required placeholder = "Street"/> 
 +          </td>
         </tr>         </tr>
         <tr>         <tr>
           <td> City, State, Zip: </td>           <td> City, State, Zip: </td>
-          <td> <input type = "text" name = "city" +          <td> 
-                      size = "30" /></td>+          <input type = "text" name = "city" 
 +          size = "30" required placeholder="City, State, Zip"/> 
 +          </td>
         </tr>         </tr>
       </table>       </table>
       <p />       <p />
       <table border = "border">       <table border = "border">
- +        <!-- First, the column headings -->
-<!-- First, the column headings --> +
         <tr>         <tr>
           <th> Product </th>           <th> Product </th>
Line 294: Line 388:
           <th> Quantity </th>           <th> Quantity </th>
         </tr>         </tr>
- +        <!-- Now, the table data entries -->
-<!-- Now, the table data entries --> +
         <tr>         <tr>
           <td> Unpopped Popcorn (1 lb.) </td>           <td> Unpopped Popcorn (1 lb.) </td>
           <td> $3.00 </td>           <td> $3.00 </td>
           <td align = "center">           <td align = "center">
-            <input type = "text" name = "unpop" +          <input type = "number" name = "unpop" 
-                   size = "3" /></td>+          size = "3" min="0" pattern="\d+" /> 
 +          </td>
         </tr>         </tr>
         <tr>         <tr>
Line 308: Line 401:
           <td> $3.50 </td>           <td> $3.50 </td>
           <td align = "center">           <td align = "center">
-            <input type = "text" name = "caramel" +          <input type = "number" name = "caramel" 
-                   size = "3" /> </td>+          size = "3" min="0" pattern="\d+" /> 
 +          </td>
         </tr>         </tr>
         <tr>         <tr>
Line 315: Line 409:
           <td> $4.50 </td>           <td> $4.50 </td>
           <td align = "center">           <td align = "center">
-            <input type = "text" name = "caramelnut" +          <input type = "number" name = "caramelnut" 
-                   size = "3" /> </td>+          size = "3" min="0" pattern="\d+" /> 
 +          </td>
         </tr>         </tr>
         <tr>         <tr>
Line 322: Line 417:
           <td> $5.00 </td>           <td> $5.00 </td>
           <td align = "center">           <td align = "center">
-            <input type = "text" name = "toffeynut" +          <input type = "number" name = "toffeynut" 
-                   size = "3" /> </td>+          size = "3" min="0" pattern="\d" /> 
 +          </td>
         </tr>         </tr>
       </table>       </table>
       <p />       <p />
- +      <!-- The radio buttons for the payment method -->
-<!-- The radio buttons for the payment method --> +
       <h3> Payment Method </h3>       <h3> Payment Method </h3>
       <p>       <p>
         <input type = "radio" name = "payment" value = "visa"         <input type = "radio" name = "payment" value = "visa"
-              checked = "checked" /> +        checked = "checked" /> 
-          Visa <br />+        Visa 
 +        <br />
         <input type = "radio" name = "payment" value = "mc" />         <input type = "radio" name = "payment" value = "mc" />
-          Master Card <br />+        Master Card 
 +        <br />
         <input type = "radio" name = "payment"         <input type = "radio" name = "payment"
-               value = "discover" /> +        value = "discover" /> 
-          Discover <br />+        Discover 
 +        <br />
         <input type = "radio" name = "payment" value = "check" />         <input type = "radio" name = "payment" value = "check" />
-          Check <br /> <br /> +        Check 
- +        <br /> 
-<!-- The submit and reset buttons --> +        <br /> 
 +        <!-- The submit and reset buttons -->
         <input type = "submit" value = "Submit Order" />         <input type = "submit" value = "Submit Order" />
         <input type = "reset" value = "Clear Order Form" />         <input type = "reset" value = "Clear Order Form" />
Line 362: Line 459:
 ---- ----
 <code php> <code php>
-<?php echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); ?>  +<!DOCTYPE html>
-<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1 //EN" +
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +
 <!-- popcorn3.php - Processes the form described in <!-- popcorn3.php - Processes the form described in
-     popcorn3.html +popcorn3.html 
-     --> +--> 
-<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en">+<html lang="en">
   <head>   <head>
 +    <meta charset="utf-8" />
     <title> Process the popcorn3.html form </title>     <title> Process the popcorn3.html form </title>
   </head>   </head>
   <body>   <body>
     <?php     <?php
- 
 // Get form data values // Get form data values
- +$unpop=$_POST["unpop"]; 
-      $unpop = $_POST["unpop"]; +$caramel=$_POST["caramel"]; 
-      $caramel = $_POST["caramel"]; +$caramelnut=$_POST["caramelnut"]; 
-      $caramelnut = $_POST["caramelnut"]; +$toffeynut=$_POST["toffeynut"]; 
-      $toffeynut = $_POST["toffeynut"]; +$name=$_POST["name"]; 
-      $name = $_POST["name"]; +$street=$_POST["street"]; 
-      $street = $_POST["street"]; +$city=$_POST["city"]; 
-      $city = $_POST["city"]; +$payment=$_POST["payment"];
-      $payment = $_POST["payment"]; +
 // If any of the quantities are blank, set them to zero // If any of the quantities are blank, set them to zero
- +if($unpop=="") 
-      if ($unpop == "") $unpop = 0; +$unpop=0; 
-      if ($caramel == "") $caramel = 0; +if($caramel=="") 
-      if ($caramelnut == "") $caramelnut = 0; +$caramel=0; 
-      if ($toffeynut == "") $toffeynut = 0; +if($caramelnut=="") 
 +$caramelnut=0; 
 +if($toffeynut=="") 
 +$toffeynut=0;
 // Compute the item costs and total cost // Compute the item costs and total cost
- +$unpop_cost=3.0*$unpop; 
-      $unpop_cost = 3.0 * $unpop; +$caramel_cost=3.5*$caramel; 
-      $caramel_cost = 3.5 * $caramel; +$caramelnut_cost=4.5*$caramelnut; 
-      $caramelnut_cost = 4.5 * $caramelnut; +$toffeynut_cost=5.0*$toffeynut; 
-      $toffeynut_cost = 5.0 * $toffeynut; +$total_price=$unpop_cost+$caramel_cost+$caramelnut_cost+$toffeynut_cost; 
-      $total_price = $unpop_cost + $caramel_cost +  +$total_items=$unpop+$caramel+$caramelnut+$toffeynut;
-                     $caramelnut_cost + $toffeynut_cost; +
-      $total_items = $unpop + $caramel + $caramelnut + $toffeynut; +
 // Return the results to the browser in a table // Return the results to the browser in a table
- 
     ?>     ?>
     <h4> Customer: </h4>     <h4> Customer: </h4>
     <?php     <?php
-      print ("$name <br /> $street <br /> $city <br />");+print("$name <br /> $street <br /> $city <br />");
     ?>     ?>
-    <p /> <p /> +    <p /> 
-<table border = "border"> +    <p /> 
-      <caption> Order Information </caption>+    <table border = "border"> 
 +      <caption> 
 +        Order Information 
 +      </caption>
       <tr>       <tr>
         <th> Product </th>         <th> Product </th>
Line 423: Line 516:
         <td> Unpopped Popcorn </td>         <td> Unpopped Popcorn </td>
         <td> $3.00 </td>         <td> $3.00 </td>
-        <td> <?php print ("$unpop"); ?> </td> +        <td><?php print("$unpop");?></td> 
-        <td> <?php printf ("$ %4.2f", $unpop_cost); ?> +        <td><?php printf("$ %4.2f",$unpop_cost);?></td>
-        </td>+
       </tr>       </tr>
       <tr align = "center">       <tr align = "center">
         <td> Caramel Popcorn </td>         <td> Caramel Popcorn </td>
         <td> $3.50 </td>         <td> $3.50 </td>
-        <td> <?php print ("$caramel"); ?> </td> +        <td><?php print("$caramel");?></td> 
-        <td> <?php printf ("$ %4.2f", $caramel_cost); ?> +        <td><?php printf("$ %4.2f",$caramel_cost);?></td> 
-        </td> +      </tr>
-        </tr>+
       <tr align = "center">       <tr align = "center">
         <td> Caramel Nut Popcorn </td>         <td> Caramel Nut Popcorn </td>
         <td> $4.50 </td>         <td> $4.50 </td>
-        <td> <?php print ("$caramelnut"); ?> </td> +        <td><?php print("$caramelnut");?></td> 
-        <td> <?php printf ("$ %4.2f", $caramelnut_cost); ?> +        <td><?php printf("$ %4.2f",$caramelnut_cost);?></td>
-        </td>+
       </tr>       </tr>
       <tr align = "center">       <tr align = "center">
         <td> Toffey Nut Popcorn </td>         <td> Toffey Nut Popcorn </td>
         <td> $5.00 </td>         <td> $5.00 </td>
-        <td> <?php print ("$toffeynut"); ?> </td> +        <td><?php print("$toffeynut");?></td> 
-        <td> <?php printf ("$ %4.2f", $toffeynut_cost); ?> +        <td><?php printf("$ %4.2f",$toffeynut_cost);?></td>
-        </td>+
       </tr>       </tr>
     </table>     </table>
-    <p /> <p /> +    <p /> 
 +    <p />
     <?php     <?php
-      print ("You ordered $total_items popcorn items <br />"); +print("You ordered $total_items popcorn items <br />"); 
-      printf ("Your total bill is: $ %5.2f <br />", $total_price); +printf("Your total bill is: $ %5.2f <br />",$total_price); 
-      print ("Your chosen method of payment is: $payment <br />");+print("Your chosen method of payment is: $payment <br />");
     ?>     ?>
   </body>   </body>
Line 462: Line 551:
   * Output   * Output
  
-{{eg-259:l18-popcorn3-output.png|receipt for Popcorn oder}}+<html> 
 +        <h4> Customer</h4> 
 +     <br />  <br />  <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> Unpopped Popcorn </td> 
 +        <td> $3.00 </td> 
 +        <td> 0 </td> 
 +        <td> $ 0.00        </td> 
 +      </tr> 
 +      <tr align = "center"> 
 +        <td> Caramel Popcorn </td> 
 +        <td> $3.50 </td> 
 +        <td> 0 </td> 
 +        <td> $ 0.00        </td> 
 +        </tr> 
 +      <tr align = "center"> 
 +        <td> Caramel Nut Popcorn </td> 
 +        <td> $4.50 </td> 
 +        <td> 0 </td> 
 +        <td> $ 0.00        </td> 
 +      </tr> 
 +      <tr align = "center"> 
 +        <td> Toffey Nut Popcorn </td> 
 +        <td> $5.00 </td> 
 +        <td> 0 </td> 
 +        <td> $ 0.00        </td> 
 +      </tr> 
 +    </table> 
 +    <p /> <p /> 
 + 
 +    You ordered 0 popcorn items <br />Your total bill is: $  0.00 <br />Your chosen method of payment is:  <br /> 
 +</html>
  
 ===== Files ===== ===== Files =====
eg-259/lecture18.1259074258.txt.gz · Last modified: 2011/01/14 12:28 (external edit)