Your Ad Here

Posted By

Waldio on 06/05/11


Tagged

error Handling scripts


Versions (?)

Verwerking formulier


 / Published in: PHP
 

  1. <?php
  2. $error = array();
  3.  
  4. if($_SERVER['REQUEST_METHOD'] == 'POST')
  5. { # If the form is send
  6. if(!isset($_POST['name']))
  7. { # If $_POST['name'] is empty
  8. $error[] = 'Je hebt geen naam ingevuld';
  9. }
  10. if(!isset($_POST['email']) && preg_match('/.+*?@.+?\.{2,5}/', $_POST['email']))
  11. { # If $_POST['email'] is empty
  12. $error[] = 'Je hebt geen of een foute email ingevuld';
  13. }
  14.  
  15. if(empty($error))
  16. { # If there are no errors
  17. $post = 'Hello Webmaster'."\n";
  18. $post .= 'Name: '.$_POST['name']."\n";
  19.  
  20. // Geef GELDIGE adressen op
  21. // A short description of you're site
  22. $site_name = 'Mijn Site';
  23. // You're email
  24. $own_emailadres = 'mijn_emailadres@mijn_domein.nl';
  25. // A good email for errors
  26. $error_emailadres = 'mijn_emailadres@mijn_domein.nl';
  27. // The name pf person who we send the mail to
  28. $name_sender = 'Afzender';
  29. // The mail of person who we send the mail to
  30. $email_sender = 'emailadres_van_verzender@zijn_domein.nl';
  31. // Fill in a good emailadres or make it empty
  32. $bcc_email = 'geldig_emailadres@geldig_domein.nl';
  33. // HTML mail? True/False
  34. $html = true;
  35.  
  36. // Make up de headers
  37. $headers = 'From: ' . $site_name . ' <' . $own_emailadres . '>' . PHP_EOL;
  38. $headers .= 'Reply-To: ' . $naam_sender . ' <' . $email_sender . '>' . PHP_EOL;
  39. $headers .= 'Return-Path: Mail-Error <' . $error_emailadres . '>' . PHP_EOL;
  40. $headers .= ($bcc_emailadres != '') ? 'Bcc: ' . $bcc_emailadres . PHP_EOL : '';
  41. $headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
  42. $headers .= 'X-Priority: Normal' . PHP_EOL;
  43. $headers .= ($html) ? 'MIME-Version: 1.0' . PHP_EOL : '';
  44. $headers .= ($html) ? 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL : '';
  45.  
  46. // Send the mail
  47. // The first parameter is the email to send to. The second is the subject
  48. // and the tirth is the post (who we have set up above) and the last the
  49. // headers (who we've set up)
  50. $sendMail = mail($name_sender, 'Here youre subject', $post, $headers);
  51. }
  52. }
  53. ?>
  54. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  55. "http://www.w3.org/TR/html4/strict.dtd">
  56. <html lang="nl">
  57. <head>
  58. <title>Formulier verwerking</title>
  59. </head>
  60. <body>
  61. <?php
  62. if(!empty($error))
  63. { # Als $error niet leeg is
  64. echo '<div id="error"><ul>'; // Maak een div met daarin een Unorderd list
  65. foreach($error as $e)
  66. { # Voor elke error
  67. echo '<li>'.$e.'</li>'; // Echo een list item met daarin de error
  68. }
  69. echo '</ul></div>'; // Sluit de div en de list
  70. }
  71. ?>
  72. <form action="" method="post">
  73. <label>Naam: <input type="text" name="name"></label><br />
  74. <label>Email: <input type="text" name="email"></label><br />
  75. <input type="submit" value="verzenden">
  76. </form>
  77. </body>
  78. </html>

Report this snippet  

You need to login to post a comment.