Javascript 使用空字段值发送PHPMailer表单
这已经持续了一段时间,我似乎不明白为什么。我构建了一个表单,它使用PHPMailer发送邮件,无论出于何种原因,我们都会不断收到表单中包含所有空白字段值的电子邮件。这在不同网站上的多个表单上发生,所有表单都具有相同的设置。所以它并不局限于这种特定的形式 将空白表单发送到一边,表单在测试时可以正常工作。如果我填好并发送测试,一切都会顺利通过。我不确定这些空洞的意见书是如何发生的。该表单使用javascript对大多数字段进行验证 以下是我的HTML代码:Javascript 使用空字段值发送PHPMailer表单,javascript,php,html,forms,phpmailer,Javascript,Php,Html,Forms,Phpmailer,这已经持续了一段时间,我似乎不明白为什么。我构建了一个表单,它使用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>';
}
}
?>
正如在评论中所说,它很可能是垃圾邮件机器人,也可能是访问您的网站而不输入任何内容的人。这是众所周知的 据我所知,您只是在检查$visitorEmail==是否等于零 最好对表单字段使用条件语句,同时使用PHP的 和功能。 即:如果其中一个为空,则使用| |-或运算符
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
}
或者使用&&-和运算符-如果其中一个不是空的且已设置
if(!empty($_POST['form_field']) && isset($_POST['form_field']))
{
// we are good to go, fire up your PHP/mailing code
}
else {
// do something else
}
将此逻辑应用于代码将有助于减少(如果不是消除)空字段。
正如在评论中所说,它很可能是垃圾邮件机器人,也可能是访问您的网站而不输入任何内容的人。这是众所周知的 据我所知,您只是在检查$visitorEmail==是否等于零 最好对表单字段使用条件语句,同时使用PHP的 和功能。 即:如果其中一个为空,则使用| |-或运算符
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
}
或者使用&&-和运算符-如果其中一个不是空的且已设置
if(!empty($_POST['form_field']) && isset($_POST['form_field']))
{
// we are good to go, fire up your PHP/mailing code
}
else {
// do something else
}
将此逻辑应用于代码将有助于减少(如果不是消除)空字段。
使用empty和IsSet可能是垃圾邮件机器人提交的表单为空?您的phpMailer代码是一个单独的文件吗?您从不检查您的值是否已设置/不为空,即->如果!isset$_POST['email']| |空的$_POST['email']死亡'您没有提供电子邮件';。如果直接访问此页面而不发布表单,则字段值将为空。谢谢Sean!我想我可能错过了什么。在这方面只有这么多的经验。利用empty和IsSet可能是垃圾邮件机器人提交的表单是空的?你的phpMailer代码是一个单独的文件吗?您从不检查您的值是否已设置/不为空,即->如果!isset$_POST['email']| |空的$_POST['email']死亡'您没有提供电子邮件';。如果直接访问此页面而不发布表单,则字段值将为空。谢谢Sean!我想我可能错过了什么。我在这方面只有这么多经验。太棒了,非常感谢!我相信这会有助于/解决问题。如果没有,我们将实施并跟进,但我认为它应该会起作用。@norsewulf非常欢迎你,很高兴能提供帮助,谢谢,非常感谢!我相信这会有助于/解决问题。如果没有,我们将实施并跟进,但我认为它应该会起作用。@norsewulf非常欢迎你,很高兴能提供帮助,干杯