1. <?php
  2.  
  3. require_once("php/db.php");                 /* Database Class */
  4. require_once('php/utils/is_email.php');     /* Email Validation Script */
  5.  
  6. if(!empty($_POST['newcontact'])){
  7.     $contact = new Contact();
  8. } else{
  9.     //header('Location: result.php');
  10. }
  11.  
  12. /* Class Contact */
  13. class Contact
  14. {
  15.     private $db;                        /* the database obj */
  16.  
  17.     //we have to init $errors array, as otherwise form will produce errors on missing array entry
  18.     private $errors = array(            /* holds error messages */
  19.         'aanhef' => '',
  20.         'contactpersoon' => '',
  21.         'bedrijfsnaam' => '',
  22.         'email' => '',
  23.         'telefoon' => '',
  24.         'vraag1_antwoorden' => '',
  25.         'vraag2_antwoorden' => ''
  26.     );
  27.  
  28.     private $has_errors;                /* number of errors in submitted form */
  29.  
  30.     public function __construct()
  31.     {
  32.         $this->db = new DB();
  33.         if (!empty($_POST['newcontact'])) {
  34.             $this->processNewMessage();
  35.         }
  36.     }
  37.     public function processNewMessage()
  38.     {
  39.         $aanhef             = $_POST['aanhef'];
  40.         $contactpersoon     = $_POST['contactpersoon'];
  41.         $bedrijfsnaam       = $_POST['bedrijfsnaam'];
  42.         $telefoon           = $_POST['telefoon'];
  43.         $email              = $_POST['email'];
  44.         $vraag1_antwoorden  = $_POST['vraag1_antwoorden'];
  45.         $vraag2_antwoorden  = $_POST['vraag2_antwoorden'];
  46.  
  47.         /* Server Side Data Validation */
  48.         if (empty($aanhef)) {
  49.             $this->setError('aanhef', 'Vul uw aanhef in');
  50.         }
  51.  
  52.         if (empty($contactpersoon)) {
  53.             $this->setError('contactpersoon', 'Vul uw contactpersoon in');
  54.         }
  55.  
  56.         if (empty($bedrijfsnaam)) {
  57.             $this->setError('bedrijfsnaam', 'Vul uw bedrijfsnaam in');
  58.         }
  59.  
  60.         if (empty($telefoon)) {
  61.             $this->setError('telefoon', 'Vul uw telefoon in');
  62.         }
  63.  
  64.         if (empty($vraag1_antwoorden)) {
  65.             $this->setError('vraag1_antwoorden', 'Selecteer een antwoord a.u.b.');
  66.         }
  67.  
  68.         if (empty($vraag2_antwoorden)) {
  69.             $this->setError('vraag2_antwoorden', 'Selecteer een antwoord a.u.b.');
  70.         }
  71.  
  72.         if (empty($email)) {
  73.             $this->setError('email', 'Vul uw e-mail in');
  74.         }
  75.  
  76.         /* No errors, insert in db
  77.         else*/
  78.  
  79.         if(!$this->has_errors) {
  80.             if(($ret = $this->db->dbNewMessage($aanhef, $contactpersoon, $bedrijfsnaam, $email, $telefoon, $vraag1_antwoorden, $vraag2_antwoorden)) > '') {
  81.                 //$json = array('result'         => 1);
  82.                 if (SEND_EMAIL) {
  83.                     $this->sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden);
  84.                     //This is for relocating to successful result page
  85.                     header('Location: result.php');
  86.                     exit;
  87.                 } else {
  88.                     //This will need special treatment. You have to prepare an errorpage
  89.                     //for database-related issues.
  90.  
  91.                     header("Location: database-error.html");
  92.                     exit;
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     public function sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden)
  99.     {
  100.     /* Just format the email text the way you want ... */
  101.         $message_body   = "<div style=\"font-size:12px; font-weight:normal;\">Hallo,<br><br>"
  102.         ."Het volgende bedrijf heeft zich zojuist aangemeld voor de Veiligheids Quiz:</div><br>"
  103.         ."<table cellpadding=\"1\" cellspacing=\"1\" width=\"550px\"><tr><td style=\"font-size:12px; color:#000000\">Bedrijfsnaam:</td><td style=\"font-size:12px; color:#000000\">".$bedrijfsnaam."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Aanhef:</td><td style=\"font-size:12px; color:#000000\">".$aanhef."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Contactpersoon:</td><td style=\"font-size:12px; color:#000000\">".$contactpersoon."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Telefoonnummer:</td><td style=\"font-size:12px; color:#000000\">".$telefoon."</td></tr><tr><td style=\"font-size:12px; color:#000000\">E-mail:</td><td style=\"font-size:12px; color:#000000\">".$email."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Antwoord vraag 1:</td><td style=\"font-size:12px; color:#000000\">".$vraag1_antwoorden."</td></tr><tr><td style=\"font-size:12px; color:#000000\">Antwoord vraag 2:</td><td style=\"font-size:12px; color:#000000\">".$vraag2_antwoorden."</td></tr></table><br>";
  104.  
  105.         // Geef GELDIGE adressen op
  106.         // Een korte benaming voor jouw website
  107.  
  108.         $website_naam = 'Aanmelding Quiz';
  109.         // Jouw eigen geldige emailadres
  110.         $eigen_emailadres = 'MY MAIL';
  111.         // Een geldig emailadres voor errors
  112.         $error_emailadres = 'MY MAIL';
  113.         // De naam van de verzender
  114.         $naam_verzender = ''.$bedrijfsnaam.'';
  115.         // Het geldige emailadres van de afzender
  116.         $email_verzender = ''.$email.'';
  117.         // Een geldig emailadres of helemaal leeg laten
  118.         $bcc_emailadres = '';
  119.         // HTML mail? True/False
  120.         $html = true;
  121.  
  122.         // De headers samenstellen
  123.         $headers     = 'From: ' . $website_naam . ' <' . $eigen_emailadres . '>' . PHP_EOL;
  124.         $headers    .= 'Reply-To: ' . $naam_verzender . ' <' . $email_verzender . '>' . PHP_EOL;
  125.         $headers    .= 'Return-Path: Mail-Error <' . $error_emailadres . '>' . PHP_EOL;
  126.         $headers    .= ($bcc_emailadres != '') ? 'Bcc: ' . $bcc_emailadres . PHP_EOL : '';
  127.         $headers    .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
  128.         $headers    .= 'X-Priority: Normal' . PHP_EOL;
  129.         $headers    .= ($html) ? 'MIME-Version: 1.0' . PHP_EOL : '';
  130.         $headers    .= ($html) ? 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL : '';
  131.  
  132.         mail(EMAIL_TO,MESSAGE_SUBJECT,$message_body,$headers);
  133.     }
  134.  
  135.     public function setError($field, $errmsg)
  136.     {
  137.         $this->has_errors     = true;
  138.         $this->errors[$field] = $errmsg;
  139.     }
  140.  
  141.     public function errors($field)
  142.     {
  143.         if (array_key_exists($field,$this->errors)){
  144.             return $this->errors[$field];
  145.         }
  146.        
  147.         return '';
  148.     }
  149. };
  150. ?>
  151.  
  152. <table width="675px" cellpadding="0" cellspacing="0">
  153. <form id="contact_form" method="post" action="">
  154. <label class="label_aanhef" for="aanhef_1"><input name="aanhef" id="aanhef_1" type="radio" value="Dhr." /> Dhr.</label><label class="label_aanhef" for="aanhef_2"><input name="aanhef" id="aanhef_2" type="radio"  value="Mevr." /> Mevr.</label>
  155. <span class="error"><?php echo $contact->errors('aanhef'); ?></span>
  156.  
  157. <input id="contactpersoon" name="contactpersoon" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('contactpersoon'); ?></span>
  158. <input id="bedrijfsnaam" name="bedrijfsnaam" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('bedrijfsnaam'); ?></span>
  159. <input id="email" name="email" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('email'); ?></span>
  160. <input id="telefoon" name="telefoon" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('telefoon'); ?></span>
  161.  
  162.  
  163. <label class="label_radio" for="vraag1_A"><input name="vraag1_antwoorden" id="vraag1_A" value="A. Dat is helaas fout, het goede antwoord is: C) < 1 Ohm" type="radio" />A) Geen eis</label>
  164. <label class="label_radio" for="vraag1_B"><input name="vraag1_antwoorden" id="vraag1_B" value="B. Dat is helaas fout, het goede antwoord is: C) < 1 Ohm" type="radio"  />B) < 0,1 Ohm</label>
  165. <label class="label_radio" for="vraag1_C"><input name="vraag1_antwoorden" id="vraag1_C" value="C. Gefeliciteerd dat is het goede antwoord." type="radio"  />C) < 1 Ohm</label>
  166. <label class="label_radio" for="vraag1_D"><input name="vraag1_antwoorden" id="vraag1_D" value="D. Dat is helaas fout, het goede antwoord is: C) < 1 Ohm" type="radio" />D) < 10 Ohm</label>
  167. <span id="vraag1_antwoorden" class="foutmelding_quiz">
  168. <?php echo $contact->errors('vraag1_antwoorden'); ?>
  169. </span>
  170.  
  171. <label class="label_radio" for="vraag2_A"><input name="vraag2_antwoorden" id="vraag2_A" value="A. Gefeliciteerd dat is het goede antwoord." type="radio" />A) Geen eis</label>
  172. <label class="label_radio" for="vraag2_B"><input name="vraag2_antwoorden" id="vraag2_B" value="B. Dat is helaas fout, het goede antwoord is: A)  Geen eis" type="radio" />B) < 0,1 Ohm</label>
  173. <label class="label_radio" for="vraag2_C"><input name="vraag2_antwoorden" id="vraag2_C" value="C. Dat is helaas fout, het goede antwoord is: A)  Geen eis" type="radio" />C) < 1 Ohm</label>
  174. <label class="label_radio" for="vraag2_D"><input name="vraag2_antwoorden" id="vraag2_D" value="D. Dat is helaas fout, het goede antwoord is: A)  Geen eis" type="radio" />D) < 10 Ohm</label>
  175. <span id="vraag2_antwoorden" class="foutmelding_quiz">
  176. <?php echo $contact->errors('vraag2_antwoorden'); ?>
  177. </span>
  178. <input class="button submit" type="submit" value="" /><input id="newcontact" name="newcontact" type="hidden" value="1"></input>
  179. </form>