Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:40:51+00:00 2026-06-11T17:40:51+00:00

Purpose of my code is: – Validate the form and check for empty fields

  • 0

Purpose of my code is:
– Validate the form and check for empty fields
– Send e-mail to admin
– Add data to Mysql database
– Show data on result.php

Currently im experiencing problems with showing my data on result.php

quiz.php

 <?php

require_once("php/db.php");                 /* Database Class */
require_once('php/utils/is_email.php');     /* Email Validation Script */


$contact = new Contact();


/* Class Contact */
class Contact
{
        private $db;                        /* the database obj */

        //we have to init $errors array, as otherwise form will produce errors on missing array entry
        private $errors = array(            /* holds error messages */
                'aanhef' => '',
                'contactpersoon' => '',
                'bedrijfsnaam' => '',
                'email' => '',
                'telefoon' => '',
                'vraag1_antwoorden' => '',
                'vraag2_antwoorden' => ''
        );

        private $has_errors;                /* number of errors in submitted form */

        public function __construct()
        {
                $this->db = new DB();
                if (!empty($_POST['newcontact'])) {
                        $this->processNewMessage();
                }
        }
        public function processNewMessage()
        {
                $aanhef             = $_POST['aanhef'];
                $contactpersoon     = $_POST['contactpersoon'];
                $bedrijfsnaam       = $_POST['bedrijfsnaam'];
                $telefoon           = $_POST['telefoon'];
                $email              = $_POST['email'];
                $vraag1_antwoorden  = $_POST['vraag1_antwoorden'];
                $vraag2_antwoorden  = $_POST['vraag2_antwoorden'];

                /* Server Side Data Validation */
                if (empty($aanhef)) {
                        $this->setError('aanhef', 'Vul uw aanhef in');
                }

                if (empty($contactpersoon)) {
                        $this->setError('contactpersoon', 'Vul uw contactpersoon in');
                }

                if (empty($bedrijfsnaam)) {
                        $this->setError('bedrijfsnaam', 'Vul uw bedrijfsnaam in');
                }

                if (empty($telefoon)) {
                        $this->setError('telefoon', 'Vul uw telefoon in');
                }

                if (empty($vraag1_antwoorden)) {
                        $this->setError('vraag1_antwoorden', 'Selecteer een antwoord a.u.b.');
                }

                if (empty($vraag2_antwoorden)) {
                        $this->setError('vraag2_antwoorden', 'Selecteer een antwoord a.u.b.');
                }

                if (empty($email)) {
                        $this->setError('email', 'Vul uw e-mail in');
                }

                /* No errors, insert in db
                else*/

                if(!$this->has_errors) {
                        if(($ret = $this->db->dbNewMessage($aanhef, $contactpersoon, $bedrijfsnaam, $email, $telefoon, $vraag1_antwoorden, $vraag2_antwoorden)) > '') {
                                //$json = array('result'         => 1);
                                if (SEND_EMAIL) {
                                        $this->sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden);
                                        //This is for relocating to successful result page
                                        header('Location: result.php');
                                        exit;
                                } else {
                                        //This will need special treatment. You have to prepare an errorpage
                                        //for database-related issues.

                                        header("Location: database-error.html");
                                        exit;
                                }
                        }
                }
        }

        public function sendEmail($aanhef,$contactpersoon,$bedrijfsnaam,$email,$telefoon,$vraag1_antwoorden,$vraag2_antwoorden)
        {
        /* Just format the email text the way you want ... */
                $message_body   = "<div style=\"font-size:12px; font-weight:normal;\">Hallo,<br><br>"
                ."Het volgende bedrijf heeft zich zojuist aangemeld voor de Veiligheids Quiz:</div><br>"
                ."<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>";

                // Geef GELDIGE adressen op
                // Een korte benaming voor jouw website

                $website_naam = 'Aanmelding Quiz';
                // Jouw eigen geldige emailadres
                $eigen_emailadres = 'MY MAIL';
                // Een geldig emailadres voor errors
                $error_emailadres = 'MY MAIL';
                // De naam van de verzender
                $naam_verzender = ''.$bedrijfsnaam.'';
                // Het geldige emailadres van de afzender
                $email_verzender = ''.$email.'';
                // Een geldig emailadres of helemaal leeg laten
                $bcc_emailadres = '';
                // HTML mail? True/False
                $html = true;

                // De headers samenstellen
                $headers     = 'From: ' . $website_naam . ' <' . $eigen_emailadres . '>' . PHP_EOL;
                $headers    .= 'Reply-To: ' . $naam_verzender . ' <' . $email_verzender . '>' . PHP_EOL;
                $headers    .= 'Return-Path: Mail-Error <' . $error_emailadres . '>' . PHP_EOL;
                $headers    .= ($bcc_emailadres != '') ? 'Bcc: ' . $bcc_emailadres . PHP_EOL : '';
                $headers    .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
                $headers    .= 'X-Priority: Normal' . PHP_EOL;
                $headers    .= ($html) ? 'MIME-Version: 1.0' . PHP_EOL : '';
                $headers    .= ($html) ? 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL : '';

                mail(EMAIL_TO,MESSAGE_SUBJECT,$message_body,$headers);
        }

        public function setError($field, $errmsg)
        {
                $this->has_errors     = true;
                $this->errors[$field] = $errmsg;
        }

        public function errors($field)
        {
                if (array_key_exists($field,$this->errors)){
                        return $this->errors[$field];
                }

                return '';
        }
};
?>

<table width="675px" cellpadding="0" cellspacing="0">
<form id="contact_form" method="post" action="">
<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>
<span class="error"><?php echo $contact->errors('aanhef'); ?></span>

<input id="contactpersoon" name="contactpersoon" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('contactpersoon'); ?></span>
<input id="bedrijfsnaam" name="bedrijfsnaam" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('bedrijfsnaam'); ?></span>
<input id="email" name="email" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('email'); ?></span>
<input id="telefoon" name="telefoon" maxlength="120" type="text" onFocus="window.scrollTo(0, 0);"/><span class="error"><?php echo $contact->errors('telefoon'); ?></span>


<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>
<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>
<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>
<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>
<span id="vraag1_antwoorden" class="foutmelding_quiz">
<?php echo $contact->errors('vraag1_antwoorden'); ?>
</span>

<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>
<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>
<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>
<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>
<span id="vraag2_antwoorden" class="foutmelding_quiz">
<?php echo $contact->errors('vraag2_antwoorden'); ?>
</span>
<input class="button submit" type="submit" value="" /><input id="newcontact" name="newcontact" type="hidden" value="1"></input>
</form> 

Result.php

Aanhef: <?php echo $_POST["aanhef"]; ?><br />
Contactpersoon: <?php echo $_POST["contactpersoon"]; ?><br />
Bedrijfsnaam: <?php echo $_POST["bedrijfsnaam"]; ?><br />
Telefoon: <?php echo $_POST["telefoon"]; ?><br />
E-mail: <?php echo $_POST["email"]; ?><br /> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:40:52+00:00Added an answer on June 11, 2026 at 5:40 pm

    You should replace :

    if(!empty($_POST['newcontact'])){
            $contact = new Contact();
    } else{
            //header('Location: result.php');
    }
    

    By :

    $contact = new Contact();
    

    Cause if $_POST['newcontact'] is empty, your $contact object is not set, and the if(!empty($_POST['newcontact'])) is already in your __construct() of Contact

    Add something like this to Contact to replace your redirect :

    private $display_form = true;
    
    public function getDisplayForm() {
        return $this->display_form;
    }
    

    And replace :

    header('Location: result.php');
    exit;
    

    By :

    $this->display_form = false;
    

    And below, wrap your HTML like this :

    <?php if ($contact->getDisplayForm()) { ?>
    
    <!-- Your Form -->
    
    <table width="675px" cellpadding="0" cellspacing="0">
    <form id="contact_form" method="post" action="">
    ...
    </form>
    </table>
    
    <?php } else { ?>
    
    <!-- Your Results -->
    
    Aanhef: <?php echo $_POST["aanhef"]; ?><br />
    Contactpersoon: <?php echo $_POST["contactpersoon"]; ?><br />
    Bedrijfsnaam: <?php echo $_POST["bedrijfsnaam"]; ?><br />
    Telefoon: <?php echo $_POST["telefoon"]; ?><br />
    E-mail: <?php echo $_POST["email"]; ?><br /> 
    
    <?php } ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The purpose of this code is just separate the config data. If i use
This is my code : the purpose is to add two big numbers ,First
I've got following (simplified for example purpose) code and it works: void log(const string
The purpose of this code is to pull upgrade.zip from a central server, extract
Quick question, What have I done wrong here. The purpose of this code is
Suppose i have the code snippet as follows : ( clarification purpose/not well formed
What is the purpose of these java.lang.annotation imports in this code? Why are they
One recommends me the following code apparently only in .zshrc without explaining its purpose
What is the main purpose of overloading operators in C++? In the code below,
I run the following code on a Windows platform. The purpose is the know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.