How do I redirect the error messages from my html_form_send.php back to my email.php page. Here is what I got, I have my form email.php:
email.php
<form name = 'htmlform' action = '$template/html_form_send.php'
method = 'post' class = 'form-horizontal well' >
My email.php links to my html_form_send.php which has the following code:
html_form_send.php
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "my email";
$email_subject = "my subject";
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['telephone']) || !isset($_POST['password'])) {
died('We are sorry, but there appears to be a problem \
with the form you submitted.');
// and instead redirect the user to your error page
header("Location: http://hurstblog.co.uk/contact-error");
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$password = $_POST['password']; // required
$error_message = "";
$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 />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not \
appear to be valid.<br />';
}
if (strlen($telephone) < 2) {
$error_message .= 'The telephone you entered do not \
appear to be valid.<br />';
}
if (strlen($password) < 2) {
$error_message .= 'The password 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_from) . "\n";
$email_message .= "Telephone: " . clean_string($telephone) . "\n";
$email_message .= "Password: " . clean_string($password) . "\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);
if (mail($email_to, $email_subject, $email_message, $headers)) {
header("Location: http://domain.net");
}
}
die();
?>
My question is how can I redirect the error?
html_form_send.php
// validation expected data exists
if(!isset($_POST['name'])){
header("location: email.php?error=name");
}
if(!isset($_POST['email'])){
header("location: email.php?error=email");
}
if(!isset($_POST['telephone'])){
header("location: email.php?error=telephone");
}
if(!isset($_POST['password'])){
header("location: email.php?error=password");
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$password = $_POST['password']; // required
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
header("location: email.php?error=invalid_email");
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
header("location: email.php?error=invalid_firstname");
}
if(strlen($telephone) < 2) {
header("location: email.php?error=invalid_telephone");
}
if(strlen($password) < 2) {
header("location: email.php?error=invalid_password");
}
The $_GET['error'] will contain the error ID, so in your email.php, you should put the error messages.
$error_message = "";
if($_GET['error']=='invalid_email'){
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if($_GET['error']=='invalid_firstname'){
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
//etc
if(strlen($error_message) > 0) {
echo($error_message);
}
What I always do is get the error code or assign the text for the error on the validating page to a session variable, and then just echo out that session variable on the page where you want the user to see the variable. Don't forget to include session_start(); at the beginning of each page though!
Related
Hi there and thank you very much for your help in advance!
I'd like to send a custom confiramtion email to a user after form submission.
I have no idea how to set this up in my php file. I am a very beginner - sorry for a nooby question :-/
Thanks!!
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "christian#diamond-precision-studio.com";
$email_subject = "website html form submissions";
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['email']) ||
!isset($_POST['budget']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$budget = $_POST['budget']; // 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_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First 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 .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Budget: ".clean_string($budget)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
header( "Location: https://www.diamond-precision-studio.com/new-website/thank-you.html" );
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
<?php
}
die();
?>
this line:
header( "Location: https://www.diamond-precision-studio.com/new-website/thank-you.html" );
will redirect your script to another page BEFORE you call mail function. It means, mail will never be executed. So, remove it.
I have a contact form that sends me the information that has been submitted. The problem is that it doesn't send an email to the client to confirm the submission. The complete code is listed below. If anyone can tell me what I am missing or what I should change, it would be greatly appreciated.
<?php
if(isset($_POST['Email_Address'])) {
include 'freecontactformsettings.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['Full_Name']) ||
!isset($_POST['Email_Address']) ||
!isset($_POST['Telephone_Number']) ||
!isset($_POST['Your_Message']) ||
!isset($_POST['AntiSpam'])
) {
died('Sorry, there appears to be a problem with your form submission.');
}
$full_name = $_POST['Full_Name']; // required
$email_from = $_POST['Email_Address']; // required
$telephone = $_POST['Telephone_Number']; // not required
$comments = $_POST['Your_Message']; // required
$antispam = $_POST['AntiSpam']; // required
if (isset($_POST['newsletter'])) {
$newsletter = "yes";
} else {
$newsletter = "no";
}
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(preg_match($email_exp,$email_from)==0) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($full_name) < 2) {
$error_message .= 'Your Name does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if($antispam <> $antispam_answer) {
$error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\r\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:");
return str_replace($bad,"",$string);
}
$email_message .= "Full Name: ".clean_string($full_name)."\r\n";
$email_message .= "Email: ".clean_string($email_from)."\r\n";
$email_message .= "Telephone: ".clean_string($telephone)."\r\n";
$email_message .= "Message: ".clean_string($comments)."\r\n";
$email_message .= "Newsletter: ".clean_string($newsletter)."\r\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>
if ($email->send()){
$autoemail = new PHPMailer();
$autoemail->From = "noreply#altfuels.com";
$autoemail->FromName = "Alt Fuels";
$autoemail->AddAddress($email_from->Email Address, $full_name->Full Name);
$autoemail->Subject = "Autorepsonse: We received your submission";
$autoemail->Body = "We received your submission. We will contact you soon ...";
$autoemail->Send();
}
<?php
}
die();
?>
(*note: I changed some of the newsletter code print a "no" answer to my email.)
Looks like you are referencing $thankyou which is undefined, unless it was defined in freecontactformsettings.php which you have not listed.
Does anyone have any idea how to customize the php page so that the user can see a feedback of what he has already filled in? I noticed many php scripts have a 'thank you' feedback but I want more than that. The codes for the php as shown below :
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "james#ace.com.sg";
$email_subject = "Photography Courses that I want to sign up";
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['tel']) ||
/*!isset($_POST['basic']) ||
!isset($_POST['advanced']) ||
!isset($_POST['raw']) ||
!isset($_POST['lightroom']) ||*/
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['tel']; // required
$basic = $_POST['basic']; // not required
$advanced = $_POST['advanced']; // not required
$raw = $_POST['raw']; // not required
$lightroom = $_POST['lightroom']; // not required
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$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(!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,$telephone)) {
$error_message .= 'You need to insert the telephone number.<br />';
}*/
/* if(strlen($comments) < 2) {
$error_message .= 'Insufficient Words for comments! <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 .= "\n Name: ".clean_string($name)."\n\n";
$email_message .= "Email: ".clean_string($email_from)."\n\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n\n";
$email_message .= "Courses Taking: \n\n".clean_string($basic)."\n\n" .clean_string($advanced)."\n\n" .clean_string($raw)."\n\n" .clean_string($lightroom)."\n\n";
$email_message .= "Comments: ".clean_string($comments)."\n\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 -->
<?php
echo '<h2>Thanks for filling up this form, We will get back to you by next Monday (29 Sep). Have a terrific weekend!</h2>';
echo '<p>**How do I feedback of what the user has already filled in the form here?** </p>';
?>
<?php
$redirect = 'http://www.acetraining.com.sg';
?>
<?php
}
?>
<SCRIPT LANGUAGE="JavaScript">
redirTime = "4000";
redirURL = "<?php echo $redirect ?>";
function redirTimer() {
self.setTimeout("self.location.href=redirURL;",redirTime);}
</script>
<BODY onLoad="redirTimer()">
Thanks!
Create a Variable for date in your if(isset) condition. after this line
if(isset($_POST['submit'])){
$date=date("j M \(D\)");
$date2=date("l \(j D)", strtotime($Date. ' + 3 days'));
//I added Three days. You can its +3, you can add any number of days. its up to you
Read you last commment its according to that.
I want the result to look something like this
echo 'Thanks for filling up the form This is the confiermation what had filled up:' .
'<br/>'. 'Name: '.$name.' Email: ' .$email_from.' Tel: '. $telephone .' Courses: '.
$advanced. ' '.' - '.$date.' Right Now';
the feedback that the user sees after filling up is
echo 'Thanks for filling up the form We will get beck to you by next '. $date2;
Thanks for helping. I have managed to solve the problem. This is what I had done in the php page.
echo '<h2>Thanks for filling up the form. This is the confirmation of what you had filled up:<br/><br/>' .'<br/>'. 'Name: '.$name.'<br/><br/>'.' Email: '.$email_from.'<br/><br/>'.'Tel: '.$telephone.'<br/><br/>'.' Courses: '.$basic.'<br/><br/>'.$advanced.'<br/><br/>'.$raw.'<br/><br/>'.$lightroom.'<br/><br/>'.' Comments: '.$comments.'<h2>';
It's working fine now!
I would like to keep the mail.php script separate from the thank you page and have it redirect there after the script has run. My following script is causing an error - headers already sent...
What do I need to change to get the header call to work correctly?
<?php
session_start();
if(isset($_SESSION['captcha_value']) && isset($_POST['text3'])){
if($_SESSION['captcha_value'] != $_POST['text3']){
echo "invalid captcha";
die();
}
}else{
echo "invalid captcha";
die();
}
?>
<?php require_once('../inc/header-scripts.php'); ?>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me#mysite.com";
$email_subject = "NEW MESSAGE";
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['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$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 />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "";
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 .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\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 -->
<?php
}
?>
<?php
header('location: thank-you.php');
?>
You have a lot of extra space, along with an HTML comment. Take out all but the opening and closing PHP braces, and remove the HTML comment line. Be aware that if the captcha fails, it's going to stay on that page without redirecting. You might want to save the error somehow ($_SESSION or $_GET variables) and redirect to the previous page.
<?php
session_start();
if(isset($_SESSION['captcha_value']) && isset($_POST['text3'])){
if($_SESSION['captcha_value'] != $_POST['text3']){
echo "invalid captcha";
die();
}
}else{
echo "invalid captcha";
die();
}
require_once('../inc/header-scripts.php');
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me#mysite.com";
$email_subject = "NEW MESSAGE";
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['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$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 />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "";
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 .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\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);
}
header('location: thank-you.php');
?>
I am having problems making a website with a contact form.
First of all, any email address entered it spews out as invalid.
Also, I have a expectancy for the name, and I'm not sure how to add special characters (Eg. É) and spaces for the last name.
Here is the existing PHP:
$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 .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
}
if(strlen($subject) < 3){
$error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';
}
if(strlen($message) < 2){
$error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';
}
Updated PHP for #J. Robertson:
<?php
if (isset($_POST['email'])){
// email info
$email_to ="example#example.com";
$email_subject = $_POST['subject'];
$email_from = $_POST['email'];
//error code
function died($error) {
echo "I am sorry, but there seems to be error(s) found within the form you submitted. <br/>";
echo "The error(s) will appear below: <br/> <br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these error(s). <br/>";
die();
}
//validation
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('I am sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//expected strings
$error_message = "";
$email_exp = '/[^\s]*#[a-z0-9.-]*/i';
if (!preg_match($email_exp, $email)) {
$error_message .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
}
if(strlen($subject) < 3){
$error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';
}
if(strlen($message) < 2){
$error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';
}
if(strlen($error_message) > 0 ){
died($error_message);
}
$email_message = "Form Details below.\n\n";
//sanitization
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 .= "Subject:" . clean_string($subject) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";
//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);
}
?>
I suspect the problem lies with your reg ex. Using http://gskinner.com/RegExr/ to test it against test#test.com, your reg ex does not pick up even that basic email address.
It's highly unlikely you'll come up with a regex that covers all email possibilities- it may be more worthwhile to simply check for an '#' sign, and then send a test email to the server to see if it's valid.
Otherwise, if you'd like a valid regex to test against, try
/[^\s]*#[a-z0-9.-]*/i