I'm trying to get a JQM page to send an email via a form and then redirect to a new page. I have the mail part working fine, but the redirect not so much! After the form has been submitted and mail sent, the page basically just refreshes instead of loading the new page. I've tried many different things using the code header('Location:myredirectpage'); but nothing works.
The following php code is located below my closing HTML tag at the bottom of my page.
<?php
if(isset($_POST['mr'])) {
$to = "myemailaddress";
$subject = "Subject";
$content = ""
."Survey Details"."\n\n"
."How did you hear about us: ".$_POST['mr']."\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: <myemailaddress>\r\n";
mail($to, $subject, $content, $headers);
$sendit= #mail($to, $subject, $content, $headers);
if($sendit){
header('Location:myredirectpage');
}else{ echo "Email failed to send";}
}
?>
To redirect your page after it has been sent, you need something in the following format:
header('Location: index.php');
Change index.php to the FILENAME of what you want to get to.
This can be a full URL such as http://google.com
You are sending the mail twice : mail(...) then #mail(...).
Also using #mail(...) with # is considered bad practice.
Instead of header('Location:myredirectpage'); use
echo '<script>window.location = 'yourpage'</script>'
Try this
Related
The submit button is in contact-us.html, the code is in contact-us.php.
I tried to use the header('Location: example'); with no luck... (I tried using the URL of the page as well: header('Location: http://www.example.com/contact-us.html'); )
Any thoughts?
<?php
if($_POST["submit"]) {
$recipient="example#gmail.com";
$subject="Form to email message";
$sender=$_POST["firstname"];
$senderlast=$_POST["lastname"];
$senderemail=$_POST["senderemail"];
$message=$_POST["message"];
$mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";
header('Location: /contact-us.html'); /*Something Wrong?*/
mail($recipient, $subject, $mailBody, "From: $sender <$senderemail>")
}
Have you tried to look towards the referer? PHP has a function for that.
header('Location: ' . $_SERVER['HTTP_REFERER'])
This means that if it comes from the cnotact.html, it will look how it came there and refers back. Easy to use if you have a second contact form on another page.
I need to add that though that this may not work via secure pages. (so if your run it via https) and its possible that the header may be able to be hijacked (especially if the browser did not send the request).
Are you sure you have a button with the submit name attribute? if yes maybe try this :
<?php
if($_POST["submit"]) {
$recipient="example#gmail.com";
$subject="Form to email message";
$sender=$_POST["firstname"];
$senderlast=$_POST["lastname"];
$senderemail=$_POST["senderemail"];
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= 'From: $sender <$senderemail>' . "\r\n";
$message=$_POST["message"];
$mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";
if(mail($recipient, $subject, $mailBody, $header)){
header('Location:contact-us.html'); /*Something Wrong?*/
}
}
if no then your code never enters the if block
try using JavaScript window.location to redirect
Note : wrap window.location.href('contact-us.html'); into script tag
example :
echo "<script>window.location.href('contact-us.html');</script>";
Here's how I'd do it:
I recommend using $_SESSION to store previous url like this:
page1.php
<?php
$_SESSION['previous'] = 'http://'. $_SERVER[HTTP_HOST]. $_SERVER[REQUEST_URI];
page2.php
<?php
if ($_SESSION['previous'] !== '') {
header('Location: '. $_SESSION['previous']);
}
$_SESSION kind of works like cookies, but a lot better. Easier to use as it's just using an array - more info can be found here: http://uk1.php.net/manual/en/reserved.variables.session.php
header('Location: contact-us.html'); /*Something Wrong?
I have a simple message that I send using php mail().
The code used:
//recipient info
$to = "$bookernavn <$mail>";
$from = "Visens Venner Hillerød <booking#eksample.dk>";
$subject = "Kvittering - $a_titel - ". date("j/n - Y",$a_dato);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $from" . "\r\n";
$headers .= "Reply-To: $from" . "\r\n";
$headers .= "Return-Path: $from" . "\r\n";
$headers .= "Bcc: $from" . "\r\n";
// now lets send the email.
mail($to, $subject, $mailmsg, $headers); }
For some strange reason two mails are sent each time...
Sometimes with several minutes in between...
Any ideas?
You don't check to see if the form has been submitted so a browser refresh will send the form data again and cause the mail to be sent again. This also happens when a user presses the back button.
After the email is sent you need to do a 303 redirect to prevent the re-submission. You can redirect to the same page if you'd like.
This is called the Post/Redirect/Get pattern.
mail(...);
header('Location: /some-page-php', true, 303);
exit;
an easy way to prevent this from happening is to use POST method instead of GET for the form.
<form method="post">
if (isset($_POST['submitted']))
and at the end of the mail code use a redirect that will send the browser to load using a GET method.
Not only you can then redirect your user to a OK page "mail was sent" or a error page "sorry there was a mistake, please try again", a refresh of that page open by the browser will only send a GET, not triggering the send mail function
if (empty($errors)) {
header('Location: http://www.example.com/mail_OK.html');
exit;
} else {
// passing data to the "error/retry" page
$info = array(
'msg' => $msg,
'email' => $_POST['email'],
'name' => $_POST['name']
// etc...
)
header('Location: http://www.example.com/mailform.php?'.http_build_query($info));
exit;
}
in your form you can retrieve those info
<input name="name" type="text" placeholder="Naam" class="form-control" value="<?php echo htmlspecialchars($_GET['name']); ?>">
I have a simple message that I send using php mail().
The code used:
//recipient info
$to = "$bookernavn <$mail>";
$from = "Visens Venner Hillerød <booking#eksample.dk>";
$subject = "Kvittering - $a_titel - ". date("j/n - Y",$a_dato);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $from" . "\r\n";
$headers .= "Reply-To: $from" . "\r\n";
$headers .= "Return-Path: $from" . "\r\n";
$headers .= "Bcc: $from" . "\r\n";
// now lets send the email.
mail($to, $subject, $mailmsg, $headers); }
For some strange reason two mails are sent each time...
Sometimes with several minutes in between...
Any ideas?
You don't check to see if the form has been submitted so a browser refresh will send the form data again and cause the mail to be sent again. This also happens when a user presses the back button.
After the email is sent you need to do a 303 redirect to prevent the re-submission. You can redirect to the same page if you'd like.
This is called the Post/Redirect/Get pattern.
mail(...);
header('Location: /some-page-php', true, 303);
exit;
an easy way to prevent this from happening is to use POST method instead of GET for the form.
<form method="post">
if (isset($_POST['submitted']))
and at the end of the mail code use a redirect that will send the browser to load using a GET method.
Not only you can then redirect your user to a OK page "mail was sent" or a error page "sorry there was a mistake, please try again", a refresh of that page open by the browser will only send a GET, not triggering the send mail function
if (empty($errors)) {
header('Location: http://www.example.com/mail_OK.html');
exit;
} else {
// passing data to the "error/retry" page
$info = array(
'msg' => $msg,
'email' => $_POST['email'],
'name' => $_POST['name']
// etc...
)
header('Location: http://www.example.com/mailform.php?'.http_build_query($info));
exit;
}
in your form you can retrieve those info
<input name="name" type="text" placeholder="Naam" class="form-control" value="<?php echo htmlspecialchars($_GET['name']); ?>">
On my website I have a 'Contact Us' form.
Currently, I am using a php form to have the information that has been filled out on the form to be e-mailed to my e-mail.
I wanted an echo message to come up once someone clicks on submit, but instead it's going to another page with the message there.
I just want the page to refresh.
This is the PHP Code:
<?php
//echo $emailBody;
sendEmail();
function sendEmail(){
global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "From: ". $from . "\r\n";
$headers .= "Return-Path: ".$from;
if (!mail($to, $subject, $emailBody, $headers)) {
} else {
echo "Your Message has been sent!we will contact back you in a short moment!";
}
}
?>
PHP will always send you a new page because it is a server side language. In order for this code to be executed when a user clicks the button and then the results shown to the user, the request must be sent to the server, executed, and then sent back to the user. The only way you can 'refresh' a page like you are requesting is to use a client side language such as javascript.
It is like this: When user clicks the submit button they are requesting that PHP process the request. This can only take place on the server. Because the request goes from user to server and then back to user, you will always get a new page.
Need a little more code, but typically when doing something like this, you would have the form submit back to iself and at the top of the php, do some isset(-form variable -) checking, if they are filled out then you can enter a function to process from there and subsequently echo a message to the user indicating success/failure.
I'd use:
<script type="text/javascript">
setTimeout(function(){location.reload();},1000);
</script>
<noscript>
<meta http-equiv="refresh" content="1" />
</noscript>
You can also specify this with a header (the meta-refresh only emulates this header):
header('Refresh: 1');
It is also nice to leave the user a message such as:
<div>If this page doesn't automatically refresh in one second click here.</div>
before you echo you can redirect to the source page with the message that you want to show up there
for example
if (!mail($to, $subject, $emailBody, $headers)) {
} else {
$myMsg="Your Message has been sent!we will contact back you in a short moment!";
header('location: yourpage');
}
and in your page you can check out if there is any message like this
if(isset($myMsg))
{
echo $myMsg; // in the place & style that you want
}
change your action
<form action="This is where your webpage goes" method="POST">
so this will send your page to google:
<form action="http://google.com" method="POST">
if you remove the action, it will reload the current page, like this:
<form method="POST">
just put your php in the same page as your <form>.
like the following
<?php
if($_POST['submit'] == 'send'){
sendEmail();
function sendEmail(){
global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "From: ". $from . "\r\n";
$headers .= "Return-Path: ".$from;
if (!mail($to, $subject, $emailBody, $headers)) {
} else {
echo "Your Message has been sent!we will contact back you in a short moment!";
}
}
}
//For that to work your button needs to be like this
?>
<form method="POST">
<!-- other fields here -->
<input type="submit" name="submit" value="send" />
</form>
i am working on one static website, in which there is a contact us page. Here what i want to do is when the contact form is submitted it should show the message that - Email has been sent successfully. But the problem is i am calling the html page and we cannot pass php message in html view. So is there any way to get it done.
conatctus.php
<?php
$error = '';
$mailTo = $_POST['email'];
$mailFrom = 'info#sample.com';
//$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$fullname = $_POST['username'];
$phoneno = $_POST['mobile'];
$emailaddress = $_POST['email'];
$msgsubject =$_POST['message'];
$new = "\n";
$msg = $fullname.$new.$emailaddress.$new.$phoneno.$new.$msgsubject;
$to = $email;
$subject = 'Inquiry';
$messageclient = '<div>
<p>Thank you For Inquiry.</p>
<p> We will reach back to you shortly. Have a Nice Day!</p>
<p>Company © 2013</p>
</p></div>
';
$headers = 'From: info#email.com' . "\r\n" .
'Reply-To: info#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: Company<info#email.com>\r\n";
//$res = mail($to, $subject, $message, $headers);
//$message ='Thank you For Inquiry. We will reach back to you shortly. Have a Nice Day';
mail( $mailTo , $subject, $messageclient, $headers);
$message .= "<p>Name: $fullname</p><br /><p>Contact Number : $phoneno</p><br /> <p>Email: $emailaddress</p><br /><p>Message: $msgsubject</p>";
mail( $mailFrom, $subject,$message, $headers);
header("location:home.html");
?>
Your help is much appreciated, thanks in advance.
There are a lot of problems here, but to answer your question, you can't redirect after content has been sent.
If you add ob_start() to the top of the page it will buffer the contents and allow the redirect.
Upon, further re-reading of your post, maybe I misinterpreted. It doesn't look like you are sending content which means that your redirect IS working, but what you want is to add a message after it's been redirected.
You have options.
Redirect to a static HTML page that reflects the message you want to convey.
Redirect to a PHP page that has the logic to give the user a message.
Use Ajax to send the email and don't redirect at all.
You might want to do either an ajax request on the submit to prevent changing the page or make a landing page to which you will redirect after processing the mailing function
Have this email sent function performed as an ajax call and on it's success show user the success message.
If you can configure the web server, you can change it in a way to treat html pages like php pages. For example in APACHE's httpd.conf:
AddType application/x-httpd-php .php .htm .html
Hope that works for you.