Advertisement
Guest User

origineel

a guest
Jul 26th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.86 KB | None | 0 0
  1. <?php
  2. if ( $_SERVER['REQUEST_METHOD'] == 'POST'  &&
  3.    isset($_POST['name'], $_POST['email'], $_POST['address1'], $_POST['address2'], $_POST['towncity'], $_POST['postcode']) )
  4.    {
  5.   //  Er zijn gegevens verstuurd naar deze pagina!
  6.  
  7.   //  We gaan de errors in een array bijhouden
  8.   $aErrors = array();
  9.  
  10.   //  Een naam bevat letters en spaties (minimaal 3)
  11.   if ( !isset($_POST['name']) or !preg_match( '~^[\w ]{3,}$~', $_POST['name'] ) ) {
  12.     $aErrors['name'] = 'Please fill in your name';
  13.   }
  14.  
  15.   //  Een email-adres is wat ingewikkelder
  16.   if ( !isset($_POST['email']) or !preg_match( '~^[a-z0-9][a-z0-9_.\-]*@([a-z0-9]+\.)*[a-z0-9][a-z0-9\-]+\.([a-z]{2,6})$~i', $_POST['email'] ) ) {
  17.     $aErrors['email'] = 'Please fill in your e-mail address';
  18.   }
  19.  
  20.   //  Een adres heeft letters, cijfers, spaties (minimaal 5)
  21.   if ( !isset($_POST['address1']) or !preg_match( '~^[\w\d ]{5,}$~', $_POST['address1'] ) ) {
  22.     $aErrors['address1'] = 'Please fill in your adress';
  23.   }
  24.  
  25.   //  Een adres heeft letters, cijfers, spaties (niet verplicht)
  26.   if ( !isset($_POST['address2']) or !preg_match( '~^([\w\d ]{5,})?$~', $_POST['address2'] ) ) {
  27.     $aErrors['address2'] = 'Please fill in your adress';
  28.   }
  29.  
  30.   //  Een plaatsnaam heeft letters, spaties en misschien een apostrof
  31.   if ( !isset($_POST['towncity']) or !preg_match( '~^[\w\d\' ]*$~', $_POST['towncity'] ) ) {
  32.     $aErrors['towncity'] = 'Please fill in your town/city';
  33.   }
  34.  
  35.   //  Een postcode heeft vier cijfers, eventueel een spatie, en twee cijfers
  36.   if ( !isset($_POST['postcode']) or !preg_match( '~^\d{4} ?[a-zA-Z]{2}$~', $_POST['postcode'] ) ) {
  37.     $aErrors['postcode'] = 'Please fill in your postal code';
  38.   }
  39.  
  40.   if ( count($aErrors) == 0 ) {
  41.     //  We hebben alle gegevens
  42.  
  43.     //  Gegevens verwerken!
  44.    
  45.     //  Volgende pagina aub
  46.     header('Location: http://index.php');
  47.     die();
  48.   }
  49.  
  50. }
  51.  
  52. /************************************************************************************************************
  53. * Hier kunnen we een hele grote streep trekken. Alles wat hierboven was, was verwerking van de data, acties *
  54. * bepalen etc. Alles wat hieronder staat, draait alleen maar om de uitvoer en de feedback. Niets hieronder  *
  55. * schrijft dingen naar de database, niets hierboven schrijft iets naar het scherm. Zo houden we het model   *
  56. * van de pagina gescheiden van de weergave!                                                                 *
  57. ************************************************************************************************************/
  58.  
  59. ?>
  60. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  61.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  62. <html xmlns="http://www.w3.org/1999/xhtml">
  63.   <head>
  64.     <title>Forms</title>
  65.     <style type="text/css">
  66.       .errorlist, .error input{
  67.         border: 1px solid #f00;
  68.         background: #fdd;
  69.       }
  70.       form.cmxform fieldset {
  71.         margin-bottom: 10px;
  72.       }
  73.       form.cmxform legend {
  74.         padding: 0 2px;
  75.         font-weight: bold;
  76.       }
  77.       form.cmxform label {
  78.         display: inline-block;
  79.         line-height: 1.8;
  80.         vertical-align: top;
  81.       }
  82.       form.cmxform fieldset ol {
  83.         margin: 0;
  84.         padding: 0;
  85.       }
  86.       form.cmxform fieldset li {
  87.         list-style: none;
  88.         padding: 5px;
  89.         margin: 0;
  90.       }
  91.       form.cmxform em {
  92.         font-weight: bold;
  93.         font-style: normal;
  94.         color: #f00;
  95.       }
  96.       form.cmxform label {
  97.         width: 120px; /* Width of labels */
  98.       }
  99.     </style>
  100.   </head>
  101.   <body>
  102.     <form action="contact.php" method="post" class="cmxform">
  103.       <?php
  104.       if ( isset($aErrors) and count($aErrors) > 0 ) {
  105.         print '<ul class="errorlist">';
  106.         foreach ( $aErrors as $error ) {
  107.           print '<li>' . $error . '</li>';
  108.         }
  109.         print '</ul>';
  110.       }
  111.       ?>
  112.       <p>Please complete the form below. Mandatory fields marked <em>*</em></p>
  113.       <fieldset>
  114.         <legend>Delivery Details</legend>
  115.         <ol>
  116.           <?php echo isset($aErrors['name']) ? '<li class="error">' : '<li>' ?>
  117.             <label for="name">Name<em>*</em></label>
  118.             <input id="name" name="name" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>" />
  119.           </li>
  120.           <?php echo isset($aErrors['email']) ? '<li class="error">' : '<li>' ?>
  121.             <label for="email">E-mail<em>*</em></label>
  122.             <input id="email" name="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" />
  123.           </li>
  124.           <?php echo isset($aErrors['address1']) ? '<li class="error">' : '<li>' ?>
  125.             <label for="address1">Address<em>*</em></label>
  126.             <input id="address1" name="address1" value="<?php echo isset($_POST['address1']) ? htmlspecialchars($_POST['address1']) : '' ?>" />
  127.           </li>
  128.           <?php echo isset($aErrors['address2']) ? '<li class="error">' : '<li>' ?>
  129.             <label for="address2">Address 2</label>
  130.             <input id="address2" name="address2" value="<?php echo isset($_POST['address2']) ? htmlspecialchars($_POST['address2']) : '' ?>" />
  131.           </li>
  132.           <?php echo isset($aErrors['towncity']) ? '<li class="error">' : '<li>' ?>
  133.             <label for="towncity">Town/City</label>
  134.             <input id="towncity" name="towncity" value="<?php echo isset($_POST['towncity']) ? htmlspecialchars($_POST['towncity']) : '' ?>" />
  135.           </li>
  136.           <?php echo isset($aErrors['postcode']) ? '<li class="error">' : '<li>' ?>
  137.             <label for="postcode">Postal Code<em>*</em></label>
  138.             <input id="postcode" name="postcode" value="<?php echo isset($_POST['postcode']) ? htmlspecialchars($_POST['postcode']) : '' ?>" />
  139.           </li>
  140.         </ol>
  141.         <input type="submit" value="Verstuur" />
  142.       </fieldset>
  143.     </form>
  144.   </body>
  145. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement