PHP 表单发送与空字段值


PHPMailer Form Sending with Empty Field Values

这已经持续了一段时间,我似乎不明白为什么。我已经构建了一个与 PHPMailer 一起发送的表单,无论出于何种原因,我们不断收到来自表单的所有空白字段值的电子邮件。这发生在不同网站上的多个表单上,所有表单都具有相同的设置。因此,它不仅限于这种特定形式。

撇开空白表单不谈,表单在测试时确实可以正常工作。如果我填写并发送测试,一切都会好起来的。我不确定这些空的提交是如何发生的。该表单使用 javascript 对大多数字段进行验证。

这是我的HTML代码:

                    <div class="thank-you-message"></div>
                <form id="contactForm" class="form-horizontal" action="" method="post">

                    <!-- col 1 -->
                    <div class="form-l">
                    <div class="control-group">
                        <div class="controls">
                            <input type="text" name="first-name" id="first-name" class="validate[required,custom[onlyLetterSp],length[0,100]]" placeholder="first name" />
                        </div>
                    </div><!-- /.control-group -->
                    <div class="control-group">
                        <div class="controls">
                            <input type="text" name="last-name" id="last-name" class="validate[required,custom[onlyLetterSp],length[0,100]]" placeholder="last name" />
                        </div>
                    </div><!-- /.control-group -->
                    <div class="control-group">
                        <div class="controls">
                            <input type="text" name="email" id="email" class="validate[required,custom[email]]" placeholder="email address" />
                        </div>
                    </div><!-- /.control-group -->
                    <div class="control-group">
                        <div class="controls">
                            <input class="last" type="text" name="phone" id="phone" class="validate[required,custom[phone]]" placeholder="phone number" />
                        </div>
                    </div><!-- /.control-group -->
                    </div>
                    <!--../ col 1 -->
                    <!-- col 2 -->          
                    <div class="form-r">        
                    <div class="control-group">
                        <div class="controls">
                            <textarea rows="5" name="comments" id="comments" placeholder="message"></textarea>
                        </div>
                    </div><!-- /.control-group -->
                    <div class="control-group">
                        <div class="controls">
                            <button type="submit" class="submit"><span>send</span></button>
                        </div>
                    </div><!-- /.control-group -->
                    </div>
                    <!--../ col 1 -->
                </form>

这是phpMailer代码

<?php
// REQUIRE PHPMAILER CLASS
// -----------------------------------------------------------------------------
require_once("PHPMailerAutoload.php");
$mail = new PHPMailer();

// SET POST VARIABLES
// -----------------------------------------------------------------------------
$visitorEmail=filter_var($_POST['email']);
$name=filter_var($_POST['first-name'].' '.$_POST['last-name']);
$phone=filter_var($_POST['phone']);
$comments=filter_var($_POST['comments']);

// TO ADMIN EMAIL
// -----------------------------------------------------------------------------
// Mail Headers
$mail->IsHTML(true); // Send as HTML
$mail->AddReplyTo($visitorEmail, $name);
$mail->From  = 'the@adminemail.com';
$mail->FromName = 'The Admin <the@adminemail.com>';
$mail->AddAddress("the@adminemail.com");
// Mail Subject
$mail->Subject  = "The Web Form";
// Mail Body
$mail->Body     = '<html><body>
                                    <img src="http://url.to/image.jpg" alt="Thank you for contacting us!" />
                                    <p>Someone has submitted your form.</p>
                                    <table rules="all" style="border-color: #666;" cellpadding="10">
                                    <tr style="background: #eee;"><td><strong>Sent From:</strong> </td><td>The Web Form</td></tr>
                                    <tr><td><strong>Name:</strong> </td><td>' .$name. '</td></tr>
                                    <tr><td><strong>Email:</strong> </td><td>' .$visitorEmail. '</td></tr>
                                    <tr><td><strong>Phone:</strong> </td><td>' .$phone. '</td></tr>
                                    <tr><td><strong>Comments:</strong> </td><td>' .$comments. '</td></tr>
                                    </table>
                                    </body></html>';
$mail->Send(); // Send mail to admin
$mail->ClearAddresses(); // Clear all addresses and attachments for next loop                           

// TO VISITOR EMAIL
// -----------------------------------------------------------------------------
// Mail Headers
$mail->IsHTML(true); // Send as HTML
$mail->From  = 'the@adminemail.com';
$mail->FromName = "The Admin <the@adminemail.com>";
$mail->AddAddress($visitorEmail);
// Mail Subject
$mail->Subject  = "Thank you for contacting us";
// Mail Body
$mail->Body     = '<html><body>
                                    <img src="http://url.to/image.jpg" alt="Thank you for contacting us!" />
                                    <p>Hello,</p> 
                                    <p>Thanks for contacting us. We have received your message and will get in touch with you shortly.</p> 
                                    <p>A copy of the information you provided us with has been posted below:</p>
                                    <table rules="all" style="border-color: #666;" cellpadding="10">
                                    <tr style="background: #eee;"><td><strong>Name:</strong> </td><td>' .$name. '</td></tr>
                                    <tr><td><strong>Email:</strong> </td><td>' .$visitorEmail. '</td></tr>
                                    <tr><td><strong>Phone:</strong> </td><td>' .$phone. '</td></tr>
                                    <tr><td><strong>Comments:</strong> </td><td>' .$comments. '</td></tr>
                                    </table>
                                    </body></html>';

// THANK YOU MESSAGE
// -----------------------------------------------------------------------------
if($visitorEmail == '') {
    // If email field is blank, and validation doesn't work
    echo '<h2>Error</h2><p>An error has occurred. Please reload the page and try again. Thanks!</p>';
} else {
    if(!$mail->Send()) {
        // If mail fails to send
        echo '<h2><strong>Message was not sent.</strong></h2>';
        echo '<p>Mailer error: ' . $mail->ErrorInfo . '</p>';
    } else {
        // Success
        echo '<h2>thanks for contacting us</h2> <p>We will get back to you as soon as possible.</p>
        <p>If you have any immediate questions please give us a call. </p>';
    }
}

?>

如评论中所述,很可能是垃圾邮件机器人,以及访问您网站但根本没有输入任何内容的人。这是众所周知的。

据我所知,您只是在检查if($visitorEmail == '')是否等于无。

最好对表单字段使用条件语句,同时使用 PHP 的

  • empty()isset()功能。

即:使用 || - OR运算符(如果其中一个为空(

if(!empty($_POST['form_field']) || !empty($_POST['other_form_field']))
   { 
// we are good to go, fire up your PHP/mailing code
}
else { 
   // do something else 
}

或(使用 && - AND运算符( - (如果一个不为空并设置(

if(!empty($_POST['form_field']) && isset($_POST['form_field']))
   { 
// we are good to go, fire up your PHP/mailing code
}
else { 
   // do something else 
}

  • 将此逻辑应用于代码将帮助您减少(如果不是消除(空字段。