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)";
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'];
Here is my code, not too sure why it doesn't work but it cannot be processed. I can process phpinfo() correctly.
<?php
include("tools.php");
$username = $_POST["uname"];
$email = $_POST["email"];
$pasword = $_POST["pword"];
if(isset($username) and isset($email) and isset($password)){
if(add_user_database($username, $email, $password) == TRUE){
echo "You've been added!!!";
header("location:login.php");
}else{
echo "<script>alert('Error has occurd please contact " .
"support or try again later');</script>";
header("location:register.php");
}
}else{
echo "<script>alert('Please fill in all forms');</script>";
header("location:register.php");
}
?>
From the php docs,
"Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file."
You shouldn't need the echo things there, if you really wanted those messages, you could set them as $_SESSION('statusMessage'); and then on the redirect page check if it is set, echo out something to show it, and set them to undefined.
Also, please please please make sure that input gets sanitised in add_user_database()!!
EDIT:
Helpful hints
In the check login script:
if(add_user_database()){
$_SESSION['addUserStatus'] = "Success, you have been added, woo!";
header("Location: someOtherPage.php");
}else{
$_SESSION['addUserStatus'] = "Error! Please contact support for assistance!");
header("Location: someOtherPage.php");
}
In the some other page:
if(isset($_SESSION['addUserStatus']){
echo "<script>showLoginMessage('" . $_SESSION['addUserStatus'] . "')</script>";
$_SESSION['addUserStatus'] = undefined;
}
Header already sent error
look at
http://php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent
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']);
Here is a piece of code in PHP written in a separate file called core.php and this is included in register.php
if(($retuserkey = $this->dbcontroller->dbregister($email, $contact)) > 0){
//if user was successfuly registered send an email where he can activate his account!
$this->mailer->sendForReg($email,$hash,$flname);
echo "<script>alert('An activation link is sent to your email id. Please check you spam folder too. Please follow the link to complete the registration.'); window.location = './registersuccess.php';</script>";
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.websiteaddress.com/registersuccess.php">';
$url = "http://www.websiteaddress.com/registersuccess.php";
if(!headers_sent()) {
//If headers not sent yet... then do php redirect
header('Location: '.$url);
exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
}
mail("emailaddress#gmail.com","Registration $retuserkey",$email,"Subject");
}
Emails are sent both before and after the redirect. Both the emails are received but the redirect fails.
What am I doing wrong?
header() must be called before any actual output is sent and you have echo before it.
You can't do:
header('Location: '.$url);
exit;
after something has already been echo'd out: http://php.net/manual/en/function.header.php.
If you move your echo statements so they only print when the redirect isn't happening, then you should be OK.
header('Location: http://localhost/yourFileName.php');
exit;
header() function is used /* Redirect to a different page in the current directory that was requested */
header() function sends a raw HTTP header to a client
N.B: use right url
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.";
}