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


How to prevent blank posting when page is refreshed

我需要你的帮助。

我有一个在线表单(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);
?>

非常感谢

迈克

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

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

另一个是:在数据库插入之后,您将获得最后一个插入id。

在发送电子邮件之前,您没有检查是否发生了表单提交,甚至没有检查是否有有效数据。因此,每次页面加载都会发送一封电子邮件。

将此代码包装在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);
}
?>

你也没有清理提交的数据,这使得你的脚本容易被发送垃圾邮件。