1

Users can contact us through a Bootstrap popup modal. Currently, it submits everything and echoes a success message (still in the popup modal format). I'd like, instead of echoing the message, to redirect to an entirely new page (not another popup).

I've tried using "header('Location: contact-thank-you.html'); exit();", which seems the obvious solution. However, this is just treated as another echo - it prints out the entire page (HTML and all) in the popup modal.

There are no error messages in the console.

There are no spaces above the start of the php file (which I understand can cause issues).

I'm able to solve this by redirecting via javascript, but I want to understand why this doesn't work. I'm not echoing anything else earlier on the page (although I do have an error if someone doesn't submit a required piece of info), so shouldn't header() work? Does it just not work well with Bootstrap?

The site is built in Jekyll, if that has any bearing on the issue.

Here's a rough outline of my php, with a lot of extra stuff removed for your viewing pleasure :) :

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $name = strip_tags(trim($_POST["name"]));
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);

        // Require Email
        if (empty($email)) {
            http_response_code(400); 
            exit;
        }

        // Set the email subject.
        $subject = "$name - New Contact";

        // Build the email content.
        $email_content = ""; 
        $email_content .= "Name: $name\n";
        $email_content .= "Email: $email\n";
        $email_headers = "From: $name <$email>";
        $recipient = "myname@mycompany.com";

        send_mail($recipient, $subject, $email_content, $email_headers);

        // confirm message was sent
        header('Location: contact-thank-you.html');
        exit();
    }
    // removed mailer function and other sections unessential to this question
?>
avp
  • 381
  • 1
  • 3
  • 14

2 Answers2

1

Based on:

The site is built in Jekyll...

We'll assume that the page in question is a static HTML page. In this case, your headers aren't being set by the php script. That might explain why the php function header() isn't setting the desired headers.

In the code example you share, It's important to understand how this php script is being called. My guess is that you're sending the form data as a POST, directly from the form, or using javascript/ajax to send the data to your php script and handle the response.

It's likely that you'll want to handle the success case in javascript, and handle the redirect once the php function has returned a success.

However, if are intent on handling the redirect with php's header() function, then consider moving the contact form page to a standalone .php page. (However, I'm not sure I would advise this option given the fact you've already adopted a Jekyll based site generator)

Alex Johnson
  • 1,504
  • 13
  • 20
0

actually, it may cause a problem, if you script is written with html header manual . But use jquery or js script looks more conveniently in that cases, i mean if you not use php header in controller or smth like that