简体   繁体   中英

How to stay on same page after form submission?

I have a simple PHP question.

I have a textbox where the user enters in their email and submit to be part of our mailing list. Now the textbox is on every page as it's part of our footer. The issue I am getting is that it keeps trying to relocate me to marketing-email.php (where the php that handles all of the form submission occurs'. I want the location to stay on the current page the user is on. I tried the code below with:

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

But it still takes me to marketing-email.php. Plus it gives me a syntax error of unexpected EOF.

How can I get this to work so that after submission, it stays on current page with just a parameter added at the end?

Code below:

footer.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>

marketing-email.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;
}
?>

You can just make it as function and include it to the file for submission...

marketing-email.php

Eg: function submitHandleter($data){

And write all of your logic }

In footer.php

about your form include(marketing-email.php);

And call the function

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

}

And keep the form action for the same page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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