I have a PHP contact form, when a user fills the form and hits send button the form gets redirected to the home page. I want to send a success/failure message on the home/index page while redirecting. How can I do the same?
I have an idea to store a success/failure message in a session and then call it on the home page but touching php afer a long time hence need assistance on how to achieve the same. Any ideas and thoughts will be greatly appreciated.
Form Codes (it's on a separate file than home page):
<?php if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin#hillierroaddentalclinic.com.au";
$email_subject = "Enquiry from Hiller Road Dental Clinic Website";
function died($error)
{
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if (strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string)
{
$bad = array(
"content-type",
"bcc:",
"to:",
"cc:",
"href"
);
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email) . "\n";
$email_message .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Message: " . clean_string($message) . "\n";
// create email headers
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
function Redirect($url, $permanent = false)
{
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
Redirect('http://hillierroaddentalclinic.com.au/', false); } ?>
Contact from PHP
if(mail($email_to, $email_subject, $email_message, $headers )){
header("location: index.php?status=1"); //success
}else{
header("location: index.php?status=0"); //failure
}
in index.php
if(isset($_GET['status'])){
$status = $_GET['status'];
if($status == 1){
echo "Thank you for your message";
}else if($status == 0){
echo "Unable to send message";
}
}
Getting the status value from URL in index.php, if the value is '1' then success message, if the status is '0' then the error message.
For achieving a redirection with PHP either you have to use header functionality. This will redirect to the page which you need to load. Otherwise, you can use form actions to change get redirection after form submission.
You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the declaration, for example).
header('Location: '.$URL);
Here you can pass a parameter with a header according to the status. For example when you call header you can pass a parameter for displaying the message. Pass a variable and according to the variable show appropriate message.
In your case after sending an email your page in the header and either display message from the session and reset session or pass a variable with status and checking the status of the message display appropriate message in UI
PHP Header Reference
It's not a good practice to send an email like these if you are using it in a production environment. it may blacklist your domain. Use any SMTP credentials and mailer class to do it
Related
I am trying to make a contact form using HTML and PHP. The PHP for the form is below:
<?php
if(isset($_POST['email'])) {
// Email to information
$email_to ="personalemail#email.com";
$email_subject ="Contact";
$email_from ="Person";
// Error code
function died($error) {
echo "We are sorry, but there were error(s) found with the form you submitted.";
echo "These errors appear below.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
// Validation
if(!isset($_POST['fname']) || !isset($_POST['lname']) || !isset($_POST['email']) || !isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message .= 'The email address you entered does not appear to be valid.<br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $fname)) {
$error_message .= 'The first name you entered does not appear to be valid.<br/>';
}
if(!preg_match($string_exp, $lname)) {
$error_message .= 'The last name you entered does not appear to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br/>';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string($fname) . clean_string($lname) . "\n";
$email_message .= "Email:" . clean_string($email) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";
// Create email headers
$headers = 'From: ' .$email_From . "\r\n". 'Reply-To:' . $email. "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
Thankyou for contacting me. I will be in contact with you shortly. <br/>
Please click here to go back to the main website
<?php
}
?>
The issue is that when the form is submitted, it never sends an email to my own personal email. Is there something more I have to set up with the web hosting or is the problem purely in the code? I did some research about the appropriate code to put in and have tried many different options but none seem to work.
Forms that send email are errorprone, and worse yet, if something goes wrong neither you nor the end user usually get an error message. They will just think your customer service is not responding. Things that do go wrong: webserver email server down or malconfigured, recieving mailbox full, spamfilter eat email, rules on webserver's emailserver change and throws away your mails.
I recommend a cloud hosted form, that stores the form submissions and notify you by email, but email is not the primary data storage.
Something like this perhaps, has a free plan:
http://www.wufoo.com/features/
I have read all your posts about inserting headers into a php form file in order to redirect the user to another URL AFTER the form is submitted - but I can't figure out how to do it. Below is my code. Can you show me where to put the header/redirect so that the information gets e-mailed and then the user goes to another html page?
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "pecraig#moneymovers.com";
$email_subject = "Mailing List Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you
submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['company']) ||
!isset($_POST['street']) ||
!isset($_POST['city']) ||
!isset($_POST['state']) ||
!isset($_POST['zip'])) {
died('We are sorry, but there appears to be a problem with the form you
submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$company = $_POST['company']; // required
$street = $_POST['street']; // required
$city = $_POST['city']; // required
$state = $_POST['state']; // required
$zip = $_POST['zip']; // required
$error_message = "";
$string_exp = "/^[A-Za-z0-9 .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$company)) {
$error_message .= 'The Company you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$street)) {
$error_message .= 'The Street you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$city)) {
$error_message .= 'The City you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$state)) {
$error_message .= 'The State you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$zip)) {
$error_message .= 'The Zip Code you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Response from Mailing List Page. Please enter in database.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Street: ".clean_string($street)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip: ".clean_string($zip)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thanks for subscribing to our mailing list
<?php
}
?>
Right after #mail($email_to, $email_subject, $email_message, $headers);
header('Location: nextpage.php');
Note that you will never see 'Thanks for subscribing to our mailing list'
That should be on the next page, if you echo any text you will get an error because the headers would have been already created, if you want to redirect never return any text, not even a space!
First give your input type submit a name, like this name='submitform'.
and then put this in your php file
if (isset($_POST['submitform']))
{
?>
<script type="text/javascript">
window.location = "http://www.google.com/";
</script>
<?php
}
Don't forget to change the url to yours.
If your redirect is in PHP, nothing should be echoed to the user before the redirect instruction.
See header for more info.
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
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.google.com/"
You can include your header function wherever you like, as long as NO html and/or text has been printed to standard out.
For more information and usage: http://php.net/manual/en/function.header.php
I see in your code that you echo() out some text in case of error or success. Don't do that: you can't. You can only redirect OR show the text. If you show the text you'll then fail to redirect.
Whenever you want to redirect, send the headers:
header("Location: http://www.example.com/");
Remember you cant send data to the client before that, though.
Once had this issue, thought it reasonable to share how I resolved it;
I think the way to do that in php is to use the header function as:
header ("Location: exampleFile.php");
You could just enclose that header file in an if statement so that it redirects only when a certain condition is met, as in:
if (isset($_POST['submit'])){ header("Location: exampleFile.php") }
Hope that helps.
I have read all your posts about inserting headers into a php form file in order to redirect the user to another URL AFTER the form is submitted - but I can't figure out how to do it. Below is my code. Can you show me where to put the header/redirect so that the information gets e-mailed and then the user goes to another html page?
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "pecraig#moneymovers.com";
$email_subject = "Mailing List Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you
submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['company']) ||
!isset($_POST['street']) ||
!isset($_POST['city']) ||
!isset($_POST['state']) ||
!isset($_POST['zip'])) {
died('We are sorry, but there appears to be a problem with the form you
submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$company = $_POST['company']; // required
$street = $_POST['street']; // required
$city = $_POST['city']; // required
$state = $_POST['state']; // required
$zip = $_POST['zip']; // required
$error_message = "";
$string_exp = "/^[A-Za-z0-9 .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$company)) {
$error_message .= 'The Company you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$street)) {
$error_message .= 'The Street you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$city)) {
$error_message .= 'The City you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$state)) {
$error_message .= 'The State you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$zip)) {
$error_message .= 'The Zip Code you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Response from Mailing List Page. Please enter in database.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Street: ".clean_string($street)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip: ".clean_string($zip)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thanks for subscribing to our mailing list
<?php
}
?>
Right after #mail($email_to, $email_subject, $email_message, $headers);
header('Location: nextpage.php');
Note that you will never see 'Thanks for subscribing to our mailing list'
That should be on the next page, if you echo any text you will get an error because the headers would have been already created, if you want to redirect never return any text, not even a space!
First give your input type submit a name, like this name='submitform'.
and then put this in your php file
if (isset($_POST['submitform']))
{
?>
<script type="text/javascript">
window.location = "http://www.google.com/";
</script>
<?php
}
Don't forget to change the url to yours.
If your redirect is in PHP, nothing should be echoed to the user before the redirect instruction.
See header for more info.
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
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.google.com/"
You can include your header function wherever you like, as long as NO html and/or text has been printed to standard out.
For more information and usage: http://php.net/manual/en/function.header.php
I see in your code that you echo() out some text in case of error or success. Don't do that: you can't. You can only redirect OR show the text. If you show the text you'll then fail to redirect.
Whenever you want to redirect, send the headers:
header("Location: http://www.example.com/");
Remember you cant send data to the client before that, though.
Once had this issue, thought it reasonable to share how I resolved it;
I think the way to do that in php is to use the header function as:
header ("Location: exampleFile.php");
You could just enclose that header file in an if statement so that it redirects only when a certain condition is met, as in:
if (isset($_POST['submit'])){ header("Location: exampleFile.php") }
Hope that helps.
I'm trying to get the user ip address from a php contact form, i have the following code, but i want to know is it valid to use clean_string in this way to email myself the ip address?
<?php
session_start();
if(isset($_POST['fullname'])) {
include 'freecontact2formsettings.php';
function died($error) {
echo "Sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['fullname']) ||
!isset($_POST['Address1']) ||
!isset($_POST['city']) ||
!isset($_POST['Postcode']) ||
!isset($_POST['contactnum']) ||
!isset($_POST['emailaddress'])
) {
died('Sorry, there appears to be a problem with your form submission.');
}
$ip = $_SERVER['HTTP_CLIENT_IP'];
$ansb0_from = $_POST['fullname']; // required
$ansb1_from = $_POST['Address1']; // required
$ansb3_from = $_POST['city']; // required
$ansb4_from = $_POST['Postcode']; // required
$ansb5_from = $_POST['contactnum']; // required
$ansb6_from = $_POST['emailaddress']; // required
$error_message = "";
$email_message = "PHP CONTACT FORM:\r\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:");
return str_replace($bad,"",$string);
}
$email_message .= "Forename: ".clean_string($ansb0_from)."\r\n";
$email_message .= "Address 1: ".clean_string($ansb1_from)."\r\n";
$email_message .= "City: ".clean_string($ansb3_from)."\r\n";
$email_message .= "Postcode: ".clean_string($ansb4_from)."\r\n";
$email_message .= "Contact Number: ".clean_string($ansb5_from)."\r\n";
$email_message .= "Email Address: ".clean_string($ansb6_from)."\r\n";
$email_message .="IP Address: ".clean_string($ip)."\n\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
header("Location: $thankyou");
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}
die();
?>
Also,
$ip = $_SERVER['HTTP_CLIENT_IP'];
is on the contact form script page, not the actual form.php which the user enters information on, i think thats where im going wrong right?
You don't want the IP in the form itself. That way it can be displayed, edited, and messed with. Instead, simply capture it server-side using:
$_SERVER['REMOTE_ADDR'];
Googling this question should by the way return half a billion results that are all valid. Just a quick reminder.
You want to check for both $_SERVER["REMOTE_ADDR"] and $_SERVER["HTTP_X_FORWARDED_FOR"], as the latter may be necessary if a user is behind a proxy server.
You can read more here: https://stackoverflow.com/a/3003233/666468
I've created a PHP file to work with my form on my jQuery mobile site. The form works perfectly and sends the email and errors work and all. But I keep getting the email from an (unknown sender). Subject line and email information is there. (Email coming from the form to host the host email address). Thanks for any help that can be provided.
<?php
if(isset($_POST['email'])){
// Here is the email to information
$email_to = "hostemail#email.com";
$email_subject = "Customer Service Form";
$email_from = "Company";
//error code
function died($error){
echo "We are sorry, but there were errors found with the form you submitted.";
echo "These errors appear bellow.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
//validation
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
//$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z] {2,4}$/';
//(!preg_match($email_exp, $email)) {
//$error_message .='The Email Address you entered does not appear to be valid.<br/>';
// }
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The name you entered does not seem to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The comments you entered do not appear to be valid.<br/>';
}
if(strlen($error_message) > 0 ) {
died($error_message);
}
$email_message = "Form details below. \n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string ($name) . "\n";
$email_message .= "E-Mail:" . clean_string ($email) . "\n";
$email_message .= "Message:" . clean_string ($message) . "\n";
//create email headers
$headers = 'From:' .$email_From . "\r\n" . 'Reply-To:' . $email. "\r\n" .
'X-MAILER: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success Message Goes Here -->
Thank you for contacting us. We will be in touch with you shortly. <br/>
Please Click here to go back to the contact page.
<?php
}
?>
The format for the From header is:
Display Name <email address>
For example:
Company <foo#company.com>
Right now, you're just using "Company", which is neither a valid e-mail address on its own, nor has an e-mail address at the end.