【问题标题】:Php mail function sends email every time page is refreshedphp邮件功能在每次页面刷新时发送邮件
【发布时间】:2017-12-01 11:48:19
【问题描述】:

我使用邮件功能制作了联系表格,一切正常。但是,我只是注意到,在发送电子邮件后,每次刷新页面时,即使字段为空,也会不断发送电子邮件。

我的代码如下所示:

<form role="form" method="POST">
    <br style="clear:both">
    <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
    <div class="form-group">
    <input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
        <select name="situation" id="situation">
            <option>Select Current Situation</option>
          <option class="placeholder" value="Unemployed">Unemployed</option>
          <option class="placeholder" value="Employed">Employed</option>
        </select>
    </div>
    <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

    <?php
        if (isset($_POST["submit"])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $mobile = $_POST['mobile'];
            $subject_line = $_POST['subject'];
            $situation = $_POST['situation'];
            $from = 'myemail@email.co.za'; 
            $to = 'myemail@email.co.za'; 
            $subject = 'SchoemanLaw lead ...';

            $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";

            //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";

            // set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers optional/headers 
            $headers .= "From:$from";

             // Check if name has been entered
            if (!$_POST['name']) {
                $errName = 'Please enter your name';
            }

            // Check if email has been entered and is valid
            if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                $errEmail = 'Please enter a valid email address';
            }

             // Check if mobile has been entered
            if (!$_POST['mobile']) {
                $errMobile = 'Please enter your number';
            }

            // If there are no errors, send the email
            if (!$errName && !$errEmail && !$errMobile) {
                if (mail($to,$subject,$body,$headers)) {
                    $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                    echo $result;
                } else {
                    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
                    echo $result;
                }
            }
        }   
    ?>
 </form>

如何让网站在发送电子邮件后忘记输入字段中的所有详细信息?

我尝试关注这个问题here,但我似乎无法在我的网站上使用它

【问题讨论】:

  • 将显示代码与处理代码分开;提交表单时,它会将数据发送到发送电子邮件的处理文件,完成后,它只会将header() 302 重定向到 “thank you” 页面或其他任何内容。由于处理文件只是进行重定向,因此浏览器中不会缓存任何内容,并且该页面无法重新加载。
  • 理想情况下我不想重定向到另一个页面
  • 您可以将表单和电子邮件处理放在一个文件中,但您需要 发送电子邮件的位与位不同显示“谢谢”消息 - 如果发送电子邮件的位输出 anything 到浏览器,浏览器可以缓存它并可以重新加载。

标签: php email


【解决方案1】:

当用户刷新页面时,可能会再次发布相同的参数。结果,if (isset($_POST["submit"])) 这个条件成立,每次用户重新加载时都会发送邮件。

一种解决方案是在成功完全完成后重定向到同一页面或不同页面。

即,

if (mail($to,$subject,$body,$headers)) {
                $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                echo $result;
            } 

代替上述方法,将用户重定向到同一页面或不同页面并在那里显示一条消息。如果您想显示相同的页面,您可以在查询字符串中使用标志重定向为 ?show_success_msg=true。

然后执行此操作。

if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] == 'true') {
  $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
  echo $result;
}

这里有完整的解决方案:

<?php
    // Handle PHP code always on Top of the page (ie, Not after your HTML), You cant send headers if you have any content above it.

    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $mobile = $_POST['mobile'];
        $subject_line = $_POST['subject'];
        $situation = $_POST['situation'];
        $from = 'myemail@email.co.za'; 
        $to = 'myemail@email.co.za'; 
        $subject = 'SchoemanLaw lead ...';

        $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";

        //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";

        // set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        // More headers optional/headers 
        $headers .= "From:$from";

         // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

         // Check if mobile has been entered
        if (!$_POST['mobile']) {
            $errMobile = 'Please enter your number';
        }

        // If there are no errors, send the email
        if (!$errName && !$errEmail && !$errMobile) {
            if (mail($to,$subject,$body,$headers)) {
                $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                echo $result;
            } else {
                header("Location: your_page.php?show_success_msg=true")
            }
        }
    }   
?>
<form role="form" method="POST">
<?php if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] == 
'true') {
$result='<div class="alert alert-success">Thank You ! We will be in touch 
soon</div>';
echo $result;
} ?>

<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
    <select name="situation" id="situation">
        <option>Select Current Situation</option>
      <option class="placeholder" value="Unemployed">Unemployed</option>
      <option class="placeholder" value="Employed">Employed</option>
    </select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

【讨论】:

  • 我不太了解 show_success_msg 的用法。希望您不介意使用基于我的完整 php 代码更新您的答案。理想情况下,我不希望重定向到另一个页面。感谢支持
  • 完成,希望对您有所帮助。
  • 行为还是一样的。实际上,第一次提交表单并刷新页面后,我看到一个警告说如果我刷新页面,则会重复发送数据。
  • 然后由于某种原因没有正确重定向,你看到带有查询字符串标志的新 URL 了吗?
  • 你可能想检查if (mail($to,$subject,$body,$headers)) { $result='&lt;div class="alert alert-success"&gt;Thank You ! We will be in touch soon&lt;/div&gt;'; echo $result; } else { header("Location: your_page.php?show_success_msg=true") }周围的逻辑
【解决方案2】:

重定向到 ?sent=1 而不发送任何输出。并检查“已发送”以确定是否显示成功消息。试试下面(假设你的脚本是contact.php)。还要确保

contact.php

<?php
    $result = '';

    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $mobile = $_POST['mobile'];
        $subject_line = $_POST['subject'];
        $situation = $_POST['situation'];
        $from = 'myemail@email.co.za'; 
        $to = 'myemail@email.co.za'; 
        $subject = 'SchoemanLaw lead ...';

        $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";

        //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";

        // set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        // More headers optional/headers 
        $headers .= "From:$from";

         // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

         // Check if mobile has been entered
        if (!$_POST['mobile']) {
            $errMobile = 'Please enter your number';
        }

        // If there are no errors, send the email
        if (!$errName && !$errEmail && !$errMobile) {
            if (mail($to,$subject,$body,$headers)) {
                //$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                //echo $result;
                header('Location:' . 'contact.php?sent=1');
                exit;
            } else {
                $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
                //echo $result;
            }
        }
    }

    if(isset($_GET['sent'])) {
        $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
    }   

    echo $result;
?>


<form role="form" method="POST">
    <br style="clear:both">
    <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
    <div class="form-group">
    <input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
        <select name="situation" id="situation">
            <option>Select Current Situation</option>
          <option class="placeholder" value="Unemployed">Unemployed</option>
          <option class="placeholder" value="Employed">Employed</option>
        </select>
    </div>
    <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

 </form>

【讨论】:

    【解决方案3】:

    您可以尝试将CSRF token添加到您的页面以防止重复提交。 参考这个链接:How to prevent multiple form submission on multiple clicks in PHP

    【讨论】:

      【解决方案4】:

      最简单的方法是在代码中添加转发,如下所示:

      编辑:@CD001 评论

      if (mail($to,$subject,$body,$headers)) {
        $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
        echo $result;
        // header('Location: ?successfull-submit'); exit; // this one would fail because above is an output.
        echo '<meta http-equiv="refresh" content="0; url=?successfull-submit">'; // its not a good /nice alternative but it "works".
      

      【讨论】:

      • header() 重定向将失败
      【解决方案5】:

      您可以使用会话变量并在每次请求后重置它。

      或者,防止使用 javascript 重新加载同一页面。

      或者,完成后重定向到其他页面(如果可能的话)

      【讨论】:

      • 如果你用$_POST=array(); 重置你的$_POST,它只涉及这个命令之后的代码。在页面重新加载时,POST 仍然存在并且具有所有值。
      • 正确。我将删除那部分。
      • 希望您不介意使用代码 sn-p 更新单词,以使我更容易。只是强调一下,我真的不想重定向到另一个页面
      【解决方案6】:

      只需尝试重定向到另一个页面或另一个丢弃旧 $_POST 数据的函数。

      【讨论】:

        【解决方案7】:

        尝试这样,在你的成功消息部分..

        <?php
                if (isset($_POST["submit"])) {
                    $name = $_POST['name'];
                    $email = $_POST['email'];
                    $mobile = $_POST['mobile'];
                    $subject_line = $_POST['subject'];
                    $situation = $_POST['situation'];
                    $from = 'myemail@email.co.za'; 
                    $to = 'myemail@email.co.za'; 
                    $subject = 'SchoemanLaw lead ...';
        
                    $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";
        
                    //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
        
                    // set content-type when sending HTML email
                    $headers = "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        
                    // More headers optional/headers 
                    $headers .= "From:$from";
        
                     // Check if name has been entered
                    if (!$_POST['name']) {
                        $errName = 'Please enter your name';
                    }
        
                    // Check if email has been entered and is valid
                    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                        $errEmail = 'Please enter a valid email address';
                    }
        
                     // Check if mobile has been entered
                    if (!$_POST['mobile']) {
                        $errMobile = 'Please enter your number';
                    }
        
                    // If there are no errors, send the email
                    if (!$errName && !$errEmail && !$errMobile) {
                        if (mail($to,$subject,$body,$headers)) {
                             echo "<script>alert('Mail sent Successfully');</script>";
                             echo "<script>window.location = 'contact.php';</script>";
                        } else {
                            echo "<script>alert('Mail not sent');</script>";
            echo "<script>window.location = 'contact.php';</script>";
                        }
                    }
                }   
            ?>
        

        在重定向到另一个页面时,您可以限制重复问题....

        【讨论】:

          猜你喜欢
          • 2015-01-01
          • 2017-07-08
          • 2021-01-26
          • 2011-06-25
          • 2019-05-20
          • 2014-05-23
          • 1970-01-01
          • 2012-12-12
          • 2012-02-04
          相关资源
          最近更新 更多