刷新页面时如何防止空白发布

时间:2014-06-28 14:56:16

标签: php forms email

我需要你的帮助。

我有一个在线表单(http://mmg2015.org/participationform.php),可以将数据发布到后端数据库。它的效果非常好。但我决定添加一个电子邮件代码,如下所示,这样当提交表单时,表单内容的副本将被发送到电子邮箱,以便管理员能够确认并回复。

现在,因为我添加了电子邮件代码,所以当页面/表单刷新时,表单会在我的电子邮箱中留下空白字段。没有任何内容发布到数据库,因为没有内容,但我的电子邮件不断收到表单中的空白电子邮件。

任何人都可以帮我确定发布空白的原因。

<?php
$emailSubject = 'A new application for participation!';
$webMaster = 'info@mmg2015.org';

// $email and $message are the data that is being
// posted to this page from our html contact form
$nameoforganisation = $_POST['nameoforganisation'] ;
$sector = $_POST['sector'] ;
$address = $_POST['address'] ;
$email = $_POST['p1email'] ;
$p1name = $_POST['p1name'] ;
$p1phone = $_POST['p1phone'] ;
$p1designation = $_POST['p1designation'] ;
$p1email = $_POST['p1email'] ;
$p2name = $_POST['p2name'] ;
$p2phone = $_POST['p2phone'] ;
$p2designation = $_POST['p2designation'] ;
$p2email = $_POST['p2email'] ;
$signature = $_POST['signature'] ;

$body = <<<EOD
<br><hr><br>
Name of Organisation: $nameoforganisation <br>
Sector: $sector <br>
Address: $address <br>

First Participants Details:<br>

Name: $p1name <br>
Phone: $p1phone <br>
Designation: $p1designation <br>
Email: $p1email <br>

Second Participants Details:<br>

Name: $p2name <br>
Phone: $p2phone <br>
Designation: $p2designation <br>
Email: $p2email <br>

Signature: $signature <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
?>

我非常感谢

麦克

2 个答案:

答案 0 :(得分:0)

在发送电子邮件之前,您不会检查是否发生了表单提交,或者即使您拥有有效数据也是如此。因此,每次加载页面都会导致发送电子邮件。

将此代码包装在if语句中,该语句至少检查表单提交(尽管验证您收到的数据也应该发生):

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $emailSubject = 'A new application for participation!';
    $webMaster = 'info@mmg2015.org';

    // $email and $message are the data that is being
    // posted to this page from our html contact form
    $nameoforganisation = $_POST['nameoforganisation'] ;
    $sector = $_POST['sector'] ;
    $address = $_POST['address'] ;
    $email = $_POST['p1email'] ;
    $p1name = $_POST['p1name'] ;
    $p1phone = $_POST['p1phone'] ;
    $p1designation = $_POST['p1designation'] ;
    $p1email = $_POST['p1email'] ;
    $p2name = $_POST['p2name'] ;
    $p2phone = $_POST['p2phone'] ;
    $p2designation = $_POST['p2designation'] ;
    $p2email = $_POST['p2email'] ;
    $signature = $_POST['signature'] ;

    $body = <<<EOD
    <br><hr><br>
    Name of Organisation: $nameoforganisation <br>
    Sector: $sector <br>
    Address: $address <br>

    First Participants Details:<br>

    Name: $p1name <br>
    Phone: $p1phone <br>
    Designation: $p1designation <br>
    Email: $p1email <br>

    Second Participants Details:<br>

    Name: $p2name <br>
    Phone: $p2phone <br>
    Designation: $p2designation <br>
    Email: $p2email <br>

    Signature: $signature <br>
    EOD;
    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body, $headers);
}
?>

您也不会sanitize the submitted data使您的脚本容易发送垃圾邮件。

答案 1 :(得分:0)

您可以将以下内容添加到不同的条件中:

if(isset($_POST['user_form_submit_button_name']))
{
//put your insert code and send mail from here
}

另一个是: 数据库插入后,您将获得最后一个插入ID。将条件id放入代码获取最后一个插入ID然后发送邮件。