刷新网页后,表单数据尝试重新发送

我并不真正使用 PHP,而且这样做超出了我的能力范围。我正确设置了 XAMPP 及其“sendmail”文件夹,以便我可以从我创建的网站发送电子邮件。电子邮件会发送给该人,但问题是,每当我第一次发送邮件后尝试刷新时,网页都会告诉我“您正在查找您输入的已使用信息的页面。返回到该页面可能会导致您所采取的任何行动被重复”。因此,如果我单击“继续”,页面会刷新,但邮件会再次发送。

我尝试使用标头进行重定向,但它并没有真正起作用。我收到一条错误,指出“无法修改标头信息 - 标头已发送”。我也尝试过一些AJAX功能但没有成功。我真的无法理解这个问题,我现在非常感谢一些帮助

这是我的表单 HTML

        <form method="post" action="#" class="contact-form" autocomplete="off">

            <div class="row">

                <div class="col span-1-of-3">

                    <label for="name">Name</label>

                </div>

                <div class="col span-2-of-3">

                    <input type="text" name="name" id="name" placeholder="Your name" required>

                </div>

            </div>




            <div class="row">

                <div class="col span-1-of-3">

                    <label for="email">Email</label>

                </div>

                <div class="col span-2-of-3">

                    <input type="email" name="email" id="email" placeholder="Your email" required>

                </div>

            </div>





            <div class="row">

                <div class="col span-1-of-3">

                    <label for="find-us">How did you find about us?</label>

                </div>

                <div class="col span-2-of-3">

                    <select name="find-us" id="find-us">

                        <option value="friends" selected>Friends</option>

                        <option value="search">Seach Engine</option>

                        <option value="other">Other</option>

                    </select>

                </div>

            </div>



            <div class="row">

                <div class="col span-1-of-3">

                    <label>Newsletter</label>

                </div>

                <div class="col span-2-of-3">

                    <input type="checkbox" name="newsletter" id="newsletter" checked>Yes, please

                </div>

            </div>


牛魔王的故事
浏览 156回答 1
1回答

HUWWW

您需要使用POST/REDIRECT/GET 模式将用户重定向到另一个页面或同一页面以避免这种情况。发送303 HTTP 响应将告诉浏览器替换其历史记录中的该页面,并避免重新发送已发布的数据。if (mysqli_query($connection, $register)) {    if(isset($_POST['sendmail'])){        mail($_POST['email'], 'Store' , 'Thanks for the message '.$_POST['name'].' !');        header('Location: index.php', true, 303);        exit;    }    else {        // Handle error    }}这是一个非常基本的例子。您将需要为调用添加错误检查/处理,mail()因为它可能会失败。您还应该有某种消息传递方式让用户知道电子邮件已发送。
打开App,查看更多内容
随时随地看视频慕课网APP