Reputation:
I made a PHP contact form which sends an email to me. And I would like to inform the user that the message had been sent after refreshing the page.
I was able to make a pop up only before refreshed the page, with this the problem was that if the user leaves the page before clicking ok, the message won't be sent.
This is my current code:
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject=$_POST['subject'];
$mailFrom=$_POST['mail'];
$message=$_POST['message'];
$mailTo = "[email protected]";
$headers = "From: ".$mailFrom;
$txt ="You have received an e-mail from".$name.".\n\n" .$message;
if ($name != '' && $mailFrom != '') {
if (mail ($mailTo, $subject, $message, $mailFrom)) {
echo '<p>Your message has been sent.</p>';
} else {
echo '<p>Something went wrong, go back and try again.</p>';
}
} else {
echo '<p>You need to fill in all required fields.</p>';
}
}
mail($mailTo,$subject,$txt,$headers);
header("Location: index.php") ;
?>
HTML:
<div class="modal-bg">
<div class="modal">
<form class="contact-form" action="contactform.php" method="post">
<h2>Contact Us!</h2>
<label for="name">Name: </label> </br>
<input type="text" name="name" placeholder="Your name"> </br>
<label for="email">E-mail</label> </br>
<input type="text" name="mail" placeholder="Your e-mail"> </br>
<label for="subject">Subject: </label> </br>
<input type="text" name="subject" placeholder="Subject"> </br>
<label for="message">Message: </label> </br>
<textarea name="message" class="message" placeholder="Message" rows="15" cols="50"></textarea> </br>
<button type="submit" name="submit"> SEND </button>
</form>
<span class="modal-close">X</span>
</div>
</div>
Upvotes: 0
Views: 319
Reputation:
I used a different method. Made a PHP page which says it is successful and redirects it after 5 seconds
<html>
<head>
<meta http-equiv="refresh" content="5;URL=http://address.com">
</head>
<body>
<div id="messagesent">
<p>Message has been sent successfully<p>
<p> This page will be redirect after 5 seconds. Please wait ...<p>
</div>
</body>
</html>
Upvotes: 1
Reputation: 13
Ok, so what you have there is something i have had trouble with in the past: you are redirecting with a PHP header method, which is part of a (mostly) server side language which ends all of its functions as soon as the page is loaded: what i mean by this is that the code you have there will redirect the user out of your page before alerts or messages can be given out.
What i suggest you do is a little something like this in PHP:
$alert = "this is an alert and gets set whenever something happens";
//instead of an alert you could also create the paragraph with a button which triggers the redirect.
echo
"
<script>
window.onload = function(){alert('$alert');}
//this will fire the alert and stop any other thing from happening before the alert is closed
window.location.href = "https://target_page.com";
//this redirects the user to the wanted page, NB: do use href as other methods may directly redirect without waiting for the alert to display
</script>
";
Let me know if this is what you were looking for :)
Upvotes: 0