Brendan Lesniak
Brendan Lesniak

Reputation: 2321

HTML Form + PHP, Page Redirect

I have an HTML5 form set up like so:

<form id="contact-form" action="php/email.php">
            <fieldset>
                <ul id="form">
                    <li>
                        <label for="name">Name:</label>
                        <input id="name" name="name" type="text" placeholder="First and Last Name" required />
                    </li>
                    <li>
                        <label for="email">Email:</label>
                        <input id="email" name="email" type="email" placeholder="Enter email address" />
                    </li>
                    <li>
                        <label for="message">Message:</label>
                        <textarea id="message" name="message" placeholder="Leave comments here..." required></textarea>
                    </li>
                </ul>

                <a id="back" href="index.html"><-- Home</a>
                <input type="submit" id="submit" value="Submit Form" class="button"/>
            </fieldset>
        </form>

and a small PHP script that sends me an email. I am wondering how to redirect the user to a Thank You page. Any suggestions?

Sorry, here is the PHP script:

<?php

$name = $_GET['name'];
$visitorEmail = $_GET['email'];
$message = $_GET['message'];
$email_from = "[email protected]";
$email_subject = "New Form Submission! - From: " + $visitorEmail;
$email_body = "Here is the message from $name: \n $message";    
$to = "[email protected]";
$headers = "From: $email_from \r \n";
$headers .= "Reply-To: $visitorEmail \r \n";    

mail($to, $email_subject, $email_body, $headers);

echo "Success! You've sent mail!";

?>

EDIT:

Ok, the problem that I have with this:

~/MyMobileApp/php/email.php

and all of my content:

~/MyMobileApp/

I want to redirect back to a local page, my index page that is located one folder above my php script. The problem with the header is that it can't go above the directory that it is currently in; maybe I should add a thank you html page in that folder and redirect? Or is there a better way?

Upvotes: 2

Views: 8385

Answers (3)

Jeff
Jeff

Reputation: 6663

After your php sends the email, add this php snippet:

header("HTTP/1.1 303 See Other");
header("Location: /thank-you.php");
exit();

Upvotes: 3

Surreal Dreams
Surreal Dreams

Reputation: 26380

With php, redirects are easy. After the logic of your small script is done, use the header() function.

header('Location: http://www.yourlocation.com/there/myfile.html');

This sends a redirect header that will cause the targeted page to load. You can target any address. However, the script will continue to execute, so don't assume this ends it. Any code after this will run. Also, headers have to be sent before any output, so no HTML and be sure that you don't echo anything or have any spaces before the opening <? of your script. Otherwise you'll get an error.

The manual has more about header() as well.

Upvotes: 0

Zach Rattner
Zach Rattner

Reputation: 21361

In the php script, you can set an HTTP redirect header like so:

header('Location: thanks.php');

There are a couple caveats, however:

  1. You can print no output before the header statement. Since this is an HTTP header, printing content will end the header and begin the body.
  2. This won't work in an AJAX script, since you will redirect a page that is never rendered in the browser. If you do use AJAX, you can set window.location.href to the page you want to redirect to.

Upvotes: 1

Related Questions