This question already has answers here:
Does page reload ever cause post?
(3 answers)
Closed 7 years ago.
I'm working on a Mail contact form and after I submit and refresh to message keep going to my email. How could I fix this? My code is below.
<?php
if (isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_text'])) {
$contact_name = $_POST['contact_name'];
$contact_email = $_POST['contact_email'];
$contact_text = $_POST['contact_text'];
if (!empty($contact_name) && !empty($contact_email) && !empty($contact_text)) {
$to = 'test#hotmail.nl';
$subject = 'Contact form';
$body = $contact_name."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (mail($to, $subject, $body, $headers)) {
Echo 'Thanks for contacting me. I will be in touch soon.';
} else {
echo 'Sorry, an error occurred. Please try again later.';
}
} else {
echo 'All fields are required';
}
}
?>
If you want to prevent a situation, where user resubmits the form by refreshing your page, a good choice is to redirect user to exactly the same page. This way last request in browser history is changed from POST to GET, and following refreshes simply reload the page without form resubmission.
This can be achieved with the following code:
header("HTTP/1.1 302 Moved Temporarily");
header("Location: the-url-of-the-page.php", true, 302);
header("Connection: close");
exit();
Please make sure the code is executed after processing the form (sending e-mail) but before sending any data to the browser.
You should redirect user to another page after processing form & sending email.
To redirect, you can use:
header("Location:thanks.php");
exit();
Your code should be changed to:
if (mail($to, $subject, $body, $headers)) {
header("Location:thanks.php"); //<-- Note change here
exit();
} else {
echo 'Sorry, an error occurred. Please try again later.';
}
Alternatively, in the if branch where you issue the mail and thank the user, you can unset the post vars as below,
unset($_POST['contact_name']);
Related
When i refresh the page the phpmailer always resends the email.
What i did?
Used the header("Location: home.php");
But how can i do the Location to home.php and show my error message
$error = "Thank you for message!";
if($mail->send()){
header("Location: home.php");
$error = "Thank you for message!";
} else {
$error .= "Error {$mail->ErrorInfo}";
}
the problem is that when i do the header it does not show me the error message...
<div class="text-center impact">
<?php echo $error; ?>
</div>
You can pass a GET-parameter so you can check it when the page reloads. Try this code example:
if($mail->send()){
header("Location: home.php?success");
} else {
$error .= "Error {$mail->ErrorInfo}";
}
And on your page:
<div class="text-center impact">
<?php echo isset($_GET['success']) ? "Thank you for message!" : $error; ?>
</div>
You will not see it, because your browser do redirect immediately, before you can see it.
Super simple solution will be to redirect to:
header("Location: send-confirmation.php");
with the information that message has been sent.
Of course you can do more advanced solution and pass apropiate parameter to home page or to use cookies/session on your phpmailer page to avoid duplicate sending.
You are not passing the $error variable between your pages, so when you echo it, it's not defined and you'll get no output. You need to either pass it through a URL query parameter:
header("Location: home.php?error=" . rawurlencode($error));
and then retrieve it on that page:
echo $_GET['error'];
or alternatively pass it via a session variable (probably the better choice):
$_SESSION['errors'] = $error;
header("Location: home.php");
and then:
echo $_SESSION['error'];
I can't get to send visitors to the Thank you page using ECHO method.
Please help!
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
if (($name=="")||($email==""))
{
echo "All fields are required, please fill THE FORM again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Subscription Request from Website";
mail("test#test.com", $subject, $message, $from);
echo "Location: thankyou-subscription.php";
}
}
?>
Please use the header function to redirect users.
Example:
header("Location: thankyou-subscription.php");
You should also exit after echo "All fields are ...".
echo does not redirect unless it outputs some JavaScript code or HTML Meta Refresh. If you have to redirect via only PHP, use header and also make sure nothing is sent to browser before that header, not even a blank line
echo "Location: thankyou-subscription.php";
should be
header("Location: thankyou-subscription.php");
Edit
Since you mentioned you cannot avoid echo before redirect, you can use JavaScript because PHP redirect won't work after output. You can do
echo "<script>location.href='thankyou-subscription.php';</script>";
Don't echo, instead do this:
header("Location: thankyou-subscription.php)";
I want to redirect the user after emailing me.
The code runs unless I put something after the email()-method (like the header()-method).
Any hints?
function process()
{
$msg = "Form Contents: \n\n";
foreach($this->fields as $key => $field)
$msg .= "$key : $field \n";
$to = 'mymail#gmail.com';
$subject = 'thesubject';
$from = $this->fields['email'];
mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");
header('Location: thanks.html');
}
When I run into similar problems, it's usually a missing semicolon at the end of a line or, as is the case here, missing brackets becauseforeach code block should be blocked off by {}. See documentation.
Your location should be absolute, not relative.
header("Location: /myPath/thanks.html");
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
In my case, there were those quote signs. That did it.
After header('location: ....'); you should add an exit; Otherwise the script keeps running until its end.
header('Location: thanks.html');
exit;
I need to track a button every time someone clicks on it. I created a file "out.php" to send me an email and redirect to a link outside my page. This code redirects but the mail doesn't send.
<div class="buyprod">
<a target="_blank" href="http://xxx.com/out.php?url=<?php echo urlencode($this->product['from'])?>">
<img src="http://xxx.com/buybtn.jpg" alt="buy"/>
</a>
</div>
out.php
<?php
$url = urldecode($_GET['url']);
header("Location: ".$url);
$message = "Someone clicked buy: ";
$link = $this->product['from'];
mail('xxx#xxx.com', '#Buy', $message.$link);
exit;
Anyone knows what's wrong with this code?
Thanks in advance!
You are calling a class $this->product['from'] that does not exist in out.php. This will make your file error out. Also, you need to set a 'From:' header when using mail(). see stackoverflow.com/questions/6988051/php-mail-function-headers#6988085
Change out.php to -
<?php
$url = urldecode($_GET['url']);
$message = "Someone clicked buy: ";
$link = $_GET['url'];
mail('xxx#xxx.com', '#Buy', $message.$link, 'From: email#website.com');
header("Location: ".$url);
exit;
?>
Move header() to the end, right before exit
Also, your mail function should follow the following format:
mail($to, $subject, $message, $headers);
You're calling $this->product['from'] which does doesn't exist in your out.php
Probably something simple I'm missing, but what I'm trying to do is finish up a registration form and after it sends a confirmation email, redirect it to another page. What I have so far
if($sentmail){
<redirection code>
}
else {
echo "Problem sending information, please resubmit.";
}
You want header(), and possibly an exit to stop processing.
if ($sentmail) {
header('Location: http://example.com/after_true_statement_redirected');
// exit; ???
} else {
echo "Problem sending information, please resubmit.";
}
You do this by using the header(); function.
if($sentmail){
header("Location: http://mypage.com/redirect.php");
die();
}
else {
echo "Problem sending information, please resubmit.";
}
Use die(); to stop script execution. Though the page redirects, the script still executes.
The most easy redirect call is http_redirect in PHP. Does everything you need to do for a redirect.
if ($sentmail)
{
http_redirect('new location');
}
else
{
echo "Problem sending information, please resubmit.";
}