繁体   English   中英

表单提交后如何保持在同一页面上?

[英]How to stay on same page after form submission?

我有一个简单的 PHP 问题。

我有一个文本框,用户在其中输入他们的 email 并提交成为我们邮件列表的一部分。 现在文本框在每一页上,因为它是我们页脚的一部分。 我遇到的问题是它一直试图将我重新定位到marketing-email.php(处理所有表单提交的php发生的地方'。我希望该位置留在用户所在的当前页面上。我试过了下面的代码:

header("Location: $_SERVER['HTTP_REFERER']?mailingsent=1");
die;

但它仍然需要我去marketing-email.php。 另外,它给了我一个意外 EOF 的语法错误。

我怎样才能让它工作,以便在提交后,它保留在当前页面上,最后只添加一个参数?

下面的代码:

页脚.php

<?php
$mailingsent=0;
if (isset($_GET['mailingsent'])) {
     $_GET['mailingsent'];
}
?>
<html>
<body>

...


        <section id="marketing-email">
        <form class="marketing-email-form" method="post" action="https://test.com/marketing-email.php">
        <div>
        <label for="email"><b>Stay updated on any new courses and services we have to offer by joining our mailing list</b></label><br/>
        <input type="email" id="market-email" name="market-email" required placeholder="Email"/> 
        <button type="submit" class="marketing-btn">Send</button>
        </div>
            </form>
       </section>

       <?
  if (isset($_GET['mailingsent'])) {
    echo '    <section id="mailing_list_email_sent" style="background-color:green !important;width:100%;">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <p class="tagline-content">
                        Email successfully sent to our mailing list!
                        <span class="closemailinglistemailmsg" 
                        style="color:white; 
                        font-size:1.5em;
                        float:right;">&times;</span>
                    </p>
                </div>
            </div>
        </div>
  </section>';
}
?>

...

        <script>

        // Get the modal
var modal = document.getElementById("mailing_list_email_sent");

// Get the <span> element that closes the modal
var spanMailingList = document.getElementsByClassName("closemailinglistemailmsg")[0];

// When the user clicks on <span> (x), close the modal
spanMailingList.onclick = function() {
  modal.style.display = "none";
}

    </script>

营销电子邮件.php

<?php
$errors = '';
if(
   empty($_POST['market-email']
   ))
{
    $errors .= "\n Error: email is required";
}

$email_address = $_POST['market-email'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$email_body = "Request to join mailing list: $email_address";

$email_subject = "Mailing List Metis - $email_address";
mail("test@test.com",$email_subject,$email_body);


header("Location: $_SERVER['HTTP_REFERER']?mailingsent=1");
die;
}
?>

您可以将其设置为 function 并将其包含到文件中以供提交...

营销电子邮件.php

例如:function submitHandleter($data){

并写下你所有的逻辑}

在页脚中。php

关于您的表格包括(marketing-email.php);

并致电 function

if(isset($_POST['market-email'])){
submitHandleter($_POST);

}

并保持同一页面的表单操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM