I've made the code to prevent repeating send of form. According to this code the sequence of actions is followed:
1.User submits mail form.
2.Page sends form data. Session starts. All form data are assigned to global $_SESSION variables. Mail is sent by function mail(). Variable $_SESSION['sent'] = true; is created to echo "Thank you for contacting us!".
3.Then "if" check out $_SESSION['sent'] and get that it's true...
and then the problem is arisen!
Because as I understand the next action has to be echoing "Thank you for contacting us!". And then unset() has to destroy $_SESSION['sent'] to prevent the repeat of showing "Thank ..."
But it doesn't happen and the whole contact form is shown on screen, which has to be shown only the case if $_SESSION['sent'] is not set.
So, the question is does unset($_SESSION['sent']) in if(){} destroy $_SESSION['sent'] in whole script?
<?php
/* code for mailing and redirecting to the same page */
session_start();
if ( !empty( $_POST['tosendmail'] ) ) {
$_SESSION['admin_email'] = "mymail@mail.com";
$_SESSION['subject'] = $_REQUEST['subject'];
$_SESSION['email'] = $_REQUEST['email'];
$_SESSION['message'] = $_REQUEST['message'];
$send = mail( $_SESSION['admin_email'], $_SESSION['who'], $_SESSION['message'], "From:" . $_SESSION['email'] );
/* create variable $_SESSION['sent'] to use condition if ( isset( $_SESSION['sent'] ) ) below */
if ( $send ) {
$_SESSION['sent'] = true;
}
header( "Location: " . $_SERVER["REQUEST_URI"] );
}
if ( isset( $_SESSION['sent'] ) ) {
?>
<h3 class="page-title">Thank you for contacting us!</h3>
<?php
unset( $_SESSION['sent'] );
} else {
?>
<!-- CONTACT FORM -->
<h3 class="page-title">Contact us:</h3>
<form class="contact-form" method="post">
Your name:<br>
<input type="text" name="subject" value=""><br>
E-mail:<br>
<input type="email" name="email" value=""><br>
Your message:<br>
<textarea type="text" name="message" rows="10" cols="40"></textarea><br><br>
<input type="submit" name="tosendmail" value="Send">
<input type="reset" value="Reset">
</form>
<?php }
?>