0

我的代码的目的是: - 验证表单并检查空字段 - 向管理员发送电子邮件 - 将数据添加到 Mysql 数据库 - 在 result.php 上显示数据

目前我在 result.php 上显示我的数据时遇到问题

测验.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> 

结果.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 /> 
4

3 回答 3

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

将其更改为

$contact = new Contact();

我的意思是说:删除条件 if() :

当页面未发布时,它的 b'coz 不会创建联系人类的对象。

由于这个错误来了。

于 2012-09-21T12:25:53.760 回答
0

你确定 $contacts 真的被填满了吗?我在页面的开头看到了这个声明:

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

但没有进一步检查是否实际设置了联系人。如果 POST 为空,则联系人将为空。检查contacts是否为null,如果是,不要在其上调用方法或变量。:-)

于 2012-09-21T12:28:38.087 回答
0

你应该更换:

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

经过 :

$contact = new Contact();

因为如果$_POST['newcontact']是空的,你的$contact对象没有设置,并且if(!empty($_POST['newcontact']))已经在你__construct()Contact

将类似这样的内容添加到联系人以替换您的重定向:

private $display_form = true;

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

并替换:

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

经过 :

$this->display_form = false;

在下面,像这样包装你的 HTML:

<?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 } ?>
于 2012-09-21T12:30:13.500 回答