How To Make 'Submit' Button Refresh Page? - php

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>

Related

mail sends again at refresh (PHP mail function) [duplicate]

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']); ?>">

php mail() two copies sent

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']); ?>">

submit form and load new page

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

a tags doesnt work after sending mail with php

i am trying to send an email via php using the function mail(), in the mail i want to include a link, the problem is that in my outlock it shows the link but wont open it in chrome (when i hover over the link with my mouse it gives me /blocked:\ before the link itself, and in gmail it doesnt even show the link only shows the link text as plain text. can you spot any problems?
$link = 'stackoverflow.com';
$emailTo = 'root#localhost.com';
$body = '<body>click me <br/></body>';
$headers = "MIME-Version: 1.0 \n" ;
$headers .= "From: me#gmail.com";
$headers .= "Content-Type: text/html;charset=utf-8 \n";
mail($emailTo, 'title', $body, $headers);
i would appritiate any help
EDIT: i dont know if it mean anything but when i press the link my outlock openes a locate link browser
Many a times and in my experiences with links inside a body, the From: needs to be the last header entry and adding <!DOCTYPE html> etc.
Plus, you also need an http:// call.
Try this:
<?php
$link = "http://www.stackoverflow.com";
$emailTo = 'root#localhost.com';
$title = "The title message";
$body = '<!DOCTYPE html><html><head></head><body>click me <br/></body></html>';
$headers = "MIME-Version: 1.0 \n" ;
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= "From: me#gmail.com";
mail($emailTo, $title, $body, $headers);
?>

Showing php msg on HTML page

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.

Categories