Forum Moderators: coopster

Message Too Old, No Replies

I only want the message to show up once

even when i use the "back" button

         

yellow_nemo

5:39 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



My site's layout has a two-column design. The left column is used as a navigation rail. I use an include file for that since my site has over 50 pages and the navigation rail is the same in all of them. On the navigation rail, I set up an email subscription form using php. Pretty simple so far, right?

<?php

if (isset($_POST['submit'])) {
$email = $_POST['email'];
$recipient = "abc@hotmail.com";
$subject = "Forum subscription";
$message = "";
$headers = "From: $email" . "\r\n";

mail($recipient, $subject, $message, $headers);

print "Thank your for subscribing.";


}

else {

?>

To subscribe, please enter
your email address below.

<form action="<?PHP $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="email" size="30">

<input type="submit" name="submit" value="Subscribe">

</form>

<?PHP }?>

So basically, my code says display the form if the submit button is not pressed, else display the "thank you" message.

What I don't like about it is that the "thank you" message is still there when I use the back button to go back. Like I put down my email address to subscribe then i get the "thank you" message then I click a link on the right column, which is used for content, then I use the back button to go back to the previous page. Then I still see that "thank you" message. How do I solve that? Thank You!

dhardisty

9:17 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



Well, there are a couple different possibilities, with different solutions:

1. when you use back, your browser is showing a cached page. Solution: <META HTTP-EQUIV="Pragma" CONTENT="no-cache">, and check [htmlgoodies.com...] for some browser specific tweaks

2. when you use back, the browser is re-submitting form info. possible solutions:
A. check $_SERVER['HTTP_REFERER'] (haven't tested this -- maybe it resends the form page as the refering URL)
B. do a workaround with session variables: add session_start(); then add this to your current if/else statement:
if (... &&!isset($_SESSION['mailsent'])) {
...send mail
$_SESSION['mailsent'] = 1;
} else {
if (isset($_SESSION['mailsent'])) {
unset($_SESSION['mailsent']);
}
...show form
}

best,
Dave