tags:

views:

516

answers:

4

I am relatively new to PHP, so my apologies if the answer is trivial. Lol

I wrote a simple Contact Us email form (actually a WordPress page-template file). All the code is in one file.

After the user submits the form and the email is sent, the file generates a Thank You message.

If the user reloads the Thank You page, they are prompted to "Resend the Form Data," which is why I am asking this question.

My question: How do I avoid the prompt to resend the form data and still keep all of my code (including the Thank You data) in one file?

EDIT: I've seen folks use headers( Location: ), but I don't think that will work for if I want to keep all my code in one file.

A: 

You can use javascript to post the form and show the thank you message. This way the browser never leaves the page.

Zed
A: 

Even with a header('Location: xxx'); you can still redirect it to the same page, and either set a url parameter or a session variable to distinguish what should be shown.

Ryan
+7  A: 

You could redirect to a different query.

header("Location: ?page=thankyou");

Then, you don't even need to check if the POST data was sent. Just display the thank you page if page is equal to thank you.

Chacha102
Looks good. I'm wondering, is there any more code necessary to throw the header(), or is it really that simple?
Jeff
Really that simple
Chacha102
For those interested, I've been told I should include the full url in the Location and also include an exit; after throwing the header().
Jeff
A: 

Although I question your requirement to have all the code in one file (why couldn't you separate it, and use require_once to include shared library code?), the header('Location:') technique is still completely valid. Simply do:

header('Location: http://www.example.com/path/to/my-one-file-of-code.php?thankyou=1');

Then, in your file, you can have:

if (isset($_GET['thankyou']) && $_GET['thankyou']) {
    // Do whatever it is you do to thank the visitor.
}
VoteyDisciple
The second parameter will generate an Error for every other page.
Chacha102
too much php