Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ( $_SERVER['REQUEST_METHOD'] == 'POST' &&
- isset($_POST['name'], $_POST['email'], $_POST['address1'], $_POST['address2'], $_POST['towncity'], $_POST['postcode']) )
- {
- // Er zijn gegevens verstuurd naar deze pagina!
- // We gaan de errors in een array bijhouden
- $aErrors = array();
- // Een naam bevat letters en spaties (minimaal 3)
- if ( !isset($_POST['name']) or !preg_match( '~^[\w ]{3,}$~', $_POST['name'] ) ) {
- $aErrors['name'] = 'Please fill in your name';
- }
- // Een email-adres is wat ingewikkelder
- 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'] ) ) {
- $aErrors['email'] = 'Please fill in your e-mail address';
- }
- // Een adres heeft letters, cijfers, spaties (minimaal 5)
- if ( !isset($_POST['address1']) or !preg_match( '~^[\w\d ]{5,}$~', $_POST['address1'] ) ) {
- $aErrors['address1'] = 'Please fill in your adress';
- }
- // Een adres heeft letters, cijfers, spaties (niet verplicht)
- if ( !isset($_POST['address2']) or !preg_match( '~^([\w\d ]{5,})?$~', $_POST['address2'] ) ) {
- $aErrors['address2'] = 'Please fill in your adress';
- }
- // Een plaatsnaam heeft letters, spaties en misschien een apostrof
- if ( !isset($_POST['towncity']) or !preg_match( '~^[\w\d\' ]*$~', $_POST['towncity'] ) ) {
- $aErrors['towncity'] = 'Please fill in your town/city';
- }
- // Een postcode heeft vier cijfers, eventueel een spatie, en twee cijfers
- if ( !isset($_POST['postcode']) or !preg_match( '~^\d{4} ?[a-zA-Z]{2}$~', $_POST['postcode'] ) ) {
- $aErrors['postcode'] = 'Please fill in your postal code';
- }
- if ( count($aErrors) == 0 ) {
- // We hebben alle gegevens
- // Gegevens verwerken!
- // Volgende pagina aub
- header('Location: http://index.php');
- die();
- }
- }
- /************************************************************************************************************
- * Hier kunnen we een hele grote streep trekken. Alles wat hierboven was, was verwerking van de data, acties *
- * bepalen etc. Alles wat hieronder staat, draait alleen maar om de uitvoer en de feedback. Niets hieronder *
- * schrijft dingen naar de database, niets hierboven schrijft iets naar het scherm. Zo houden we het model *
- * van de pagina gescheiden van de weergave! *
- ************************************************************************************************************/
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Forms</title>
- <style type="text/css">
- .errorlist, .error input{
- border: 1px solid #f00;
- background: #fdd;
- }
- form.cmxform fieldset {
- margin-bottom: 10px;
- }
- form.cmxform legend {
- padding: 0 2px;
- font-weight: bold;
- }
- form.cmxform label {
- display: inline-block;
- line-height: 1.8;
- vertical-align: top;
- }
- form.cmxform fieldset ol {
- margin: 0;
- padding: 0;
- }
- form.cmxform fieldset li {
- list-style: none;
- padding: 5px;
- margin: 0;
- }
- form.cmxform em {
- font-weight: bold;
- font-style: normal;
- color: #f00;
- }
- form.cmxform label {
- width: 120px; /* Width of labels */
- }
- </style>
- </head>
- <body>
- <form action="contact.php" method="post" class="cmxform">
- <?php
- if ( isset($aErrors) and count($aErrors) > 0 ) {
- print '<ul class="errorlist">';
- foreach ( $aErrors as $error ) {
- print '<li>' . $error . '</li>';
- }
- print '</ul>';
- }
- ?>
- <p>Please complete the form below. Mandatory fields marked <em>*</em></p>
- <fieldset>
- <legend>Delivery Details</legend>
- <ol>
- <?php echo isset($aErrors['name']) ? '<li class="error">' : '<li>' ?>
- <label for="name">Name<em>*</em></label>
- <input id="name" name="name" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>" />
- </li>
- <?php echo isset($aErrors['email']) ? '<li class="error">' : '<li>' ?>
- <label for="email">E-mail<em>*</em></label>
- <input id="email" name="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" />
- </li>
- <?php echo isset($aErrors['address1']) ? '<li class="error">' : '<li>' ?>
- <label for="address1">Address<em>*</em></label>
- <input id="address1" name="address1" value="<?php echo isset($_POST['address1']) ? htmlspecialchars($_POST['address1']) : '' ?>" />
- </li>
- <?php echo isset($aErrors['address2']) ? '<li class="error">' : '<li>' ?>
- <label for="address2">Address 2</label>
- <input id="address2" name="address2" value="<?php echo isset($_POST['address2']) ? htmlspecialchars($_POST['address2']) : '' ?>" />
- </li>
- <?php echo isset($aErrors['towncity']) ? '<li class="error">' : '<li>' ?>
- <label for="towncity">Town/City</label>
- <input id="towncity" name="towncity" value="<?php echo isset($_POST['towncity']) ? htmlspecialchars($_POST['towncity']) : '' ?>" />
- </li>
- <?php echo isset($aErrors['postcode']) ? '<li class="error">' : '<li>' ?>
- <label for="postcode">Postal Code<em>*</em></label>
- <input id="postcode" name="postcode" value="<?php echo isset($_POST['postcode']) ? htmlspecialchars($_POST['postcode']) : '' ?>" />
- </li>
- </ol>
- <input type="submit" value="Verstuur" />
- </fieldset>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement