簡體   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