How can I change the location of my error, messages? - php

How do I redirect the error messages from my html_form_send.php back to my registration.php page. Here is what I got, I have my registration.php with the following code:
registration.php (location : /root/ )
<!-- register -->
<div>";
include "$template/email.php";
echo "
</div>
<!-- register -->
My email.php links to my html_form_send.php which has the following code:
email.php (location : /root/models/template )
<form name='htmlform' action='$template/html_form_send.php' method='post' class='form-horizontal well' >
<fieldset>
<legend>Register for an account</legend>
<!-- placeholder for errors -->
<!-- placeholder for errors -->
<br>
<div class='control-group'>
<div class='control-label'>
<label>Name</label>
</div>
<div class='controls'>
<input type='text' placeholder='Type name' name='name' class='input-large'>
My html_form_send.php has the following code for an email client that I am using. I want to show the errors on my registration page.
registration.php (location : /root/models/template )
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "danielobo2#yahoo.com";
$email_subject = "Registering to blanky-store.net web design account.";
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. <a href='../../../register.php'>Return to previous page</a><br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors. <a href='../../../register.php'>Return to previous page</a><br /><br />";
echo "<a href='../../../register.php'>Return to previous page</a><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.');
}
$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 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://blanky-store.net/index.php");
}
?>
<?php
}
die();
?>
What I want to do is display the error messages that are appearing on the html_form_send.php and send them to my registration.php instead of displaying on my html_form_send.php page. I have an example of what is doing at this following page http://blanky-store.net/access/register.php
also what would be the code to combine the email.php page and the html_form_send.php form into one php page?

Store the error message in a session and then redirect the page to registration.php
Hence check for the session if is set or not. if error message is set then display the session value and after that unset the session value..
Hope it helps..

Related

Can't find error PHP

So, this is my first attempt in PHP so it might be very easy to fix. I am trying to make a form which can be submitted and sent to administration's e-mail.
I am running a server with MAMP, going online filling the form, submitting it and I get error:
We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.
We are sorry, but there appears to be a problem with the form you submitted.
Please go back and fix these errors.
Full PHP code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "I HAVE CORRECT EMAIL HERE 4 SURE";
$email_subject = "eShop Contact 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['fname']) ||
!isset($_POST['lname']) ||
!isset($_POST['email']) ||
!isset($_POST['subject'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['fname']; // required
$last_name = $_POST['lname']; // required
$email_from = $_POST['email']; // required
//$country = $_POST['country']; // not required
$comments = $_POST['subject']; // 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(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last 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 = "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 .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
// $email_message .= "Country: ".clean_string($country)."\n";
$email_message .= "Subject: ".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 -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
This is the part that should be having problem:
// validation expected data exists
if(!isset($_POST['fname']) ||
!isset($_POST['lname']) ||
!isset($_POST['email']) ||
!isset($_POST['subject'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
P.S I have copied the code from http://www.freecontactform.com/email_form.php here and edited it a little.
Well, now I will start from here.
I will re-edit your code
SUBMIT.PHP
<?php
if(isset($_POST['formid'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "yourmail.com.id#maisetting.com";
$email_subject = "eShop Contact Form";
//
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comm'];
// I was replace this line
// validation expected data exists
if(empty($fname) or empty($lname) or empty($mail) or empty($subject) or empty($comments)){
$err_msg = 'We are sorry, but there appears to be a problem with the form you submitted.';
} else {
// check mail registered
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$mail)) {
$err_msg = 'The Email Address you entered does not appear to be valid.<br />';
} else {
// check name
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$fname)) {
$err_msg = 'The First Name you entered does not appear to be valid.<br />';
} else {
// check Last name
if(!preg_match($string_exp,$lname)) {
$err_msg = 'The Last Name you entered does not appear to be valid.<br />';
} else {
// Check comment
if($comments<2 or $comments=='') {
$err_msg = 'The Comments you entered do not appear to be valid.<br />';
} else {
// SEND msg from the visitor
$err_msg = 'Your Messages Was Send. ThankYou!';
$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($fname)."\n";
$email_message .= "Last Name: ".clean_string($lname)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
// $email_message .= "Country: ".clean_string($country)."\n";
$email_message .= "Subject: ".clean_string($comments)."\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);
}}}}} } else { $err_msg = 'Add Your comment here'; }
?>
INDEX.PHP
<?php require('SUBMIT.PHP'); ?>
<form action="#" method="post" />
<p><label>First Name</label>
<input type="text" placeholder="First name here" name="fname" />
</p>
<p><label>Last Name</label>
<input type="text" placeholder="Last name here" name="lname" />
</p>
<p><label>Mail</label>
<input type="text" placeholder="Add yourmail here" name="mail" />
</p>
<p><label>Subject</label>
<input type="text" placeholder="Subject here" name="subject" />
</p>
<p><label>Your Comment</label>
<textarea name="comm"></textarea>
</p>
<p><input type="submit" name="formid" value="Send" /></p>
</form>
<?php echo $err_msg; ?>
Now, do and test.

php form submission is working but erroring instead of redirecting [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Can someone check this php code and tell me why I'm getting this error:
"Warning: Cannot modify header information - headers already sent by (output started at /home/itechcom/public_html/DesignsbyGabe.com/send_form_email.php:144) in /home/itechcom/public_html/DesignsbyGabe.com/send_form_email.php on line 146"
PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "Jonathansumner90#gmail.com";
$email_subject = "contact from Designs by Gabe 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['comments'])) {
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']; // 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(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last 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 = "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 .= "Last Name: ".clean_string($last_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
header("Location: thankyou.html");
?>
<?php
}
?>
HTML:
<form action="send_form_email.php" method="post" name="contactform">
<label for="first_name">First Name *</label>
<input type="text" name="first_name" size="30" maxlength="50" />
<label for="last_name">Last Name *</label>
<input type="text" name="last_name" size="30" maxlength="50" />
<label for="email">Email Address *</label>
<input type="text" name="email" size="30" maxlength="80" />
<label for="telephone">Telephone Number</label>
<input type="text" name="telephone" size="30" maxlength="30" />
<label for="comments">Comments *</label>
<textarea name="comments" rows="6" cols="25"></textarea>
<input id="submit" style="margin-right: 30px;" type="submit" value="Submit" />
This question has been asked before but not in this context. The messages go through but instead of redirecting it gives me an error. I use this same form submission code for other sites and it works fine.
Try this one :
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "Jonathansumner90#gmail.com";
$email_subject = "contact from Designs by Gabe 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['comments'])) {
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']; // 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(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last 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 = "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 .= "Last Name: ".clean_string($last_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: thankyou.html");
}
?>
Put the header command inside the first PHP tags
#mail($email_to, $email_subject, $email_message, $headers);
header("Location: thankyou.html");
?>
You are doing it wrong. You can't set header after outputting something(not even a space). Just set headers on top like
<?php
header("Location: thankyou.html");
or use html meta redirect
<META http-equiv="refresh" content="0;URL=http://example.com/thankyou.html">
or use javascript
window.location.replace("http://example.com/thankyou.html");
Hope this helps you
It was an issue with The white space before the opening php tag surrounding the header. I have to assume it's the server I'm on being picky. I've heard of white space issues but i've never encountered one and I've used this form many times.
Thanks everyone for helping me out.
use exit after header('location:..'); when you redirecting to some page
like
header("Location: thankyou.html");
exit;
and also *Not To print any think before it*
such as you are using comment of html in php like
<!-- include your own success html here -->
remove above line or make it php comment as
// include your own success html here

php form not sending to email

Hi i have creating my form and this is the php script
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "malsl3#aol.com";
$email_subject = "KwikDrive Rentals Customer 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['firstname']) ||
!isset($_POST['lastname']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['firstname']; // required
$last_name = $_POST['lastname']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$comments = $_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(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$telephone_exp = '/^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/';
if(!preg_match($telephone_exp,$telephone)) {
$error_message .= 'The Telephone you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Message 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 .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".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 -->
<p>Thank you for contacting us. We will be in touch with you very soon. </p>
<p><a href="bookingform2.php">Click here if you wish to return to the contact page.<br /><br />
</a></p>
<?php
}
?>
It was working fine at the start sending to this email address but when i change email address to enquiries#kwikdrive.com it would not send. ive changed it back to malsl3#aol.com and im not recieving to that email anymore either. its says form submitted but no email comes through. malsl3 email was just for test purposes but enquiries#kwikdrive.com is the one i need tested. you can check the form at kwikdrive.com/contacts.php
Change #mail($email_to, $email_subject, $email_message, $headers); to mail($email_to, $email_subject, $email_message, $headers);
As far as I know #mail does not exist, only mail() does.

php contact form submit just takes me to action URL

I have added a simple contact form to my site, however when I click submit, I am taken to the url of the action attribute. Below is the html for my form:
<form name="quickcontact" method="post" action="includes/quick_contact.php" id="quickcontact">
<input type="text" name="first_name" maxlength="50" size="30" placeholder="Name">
<input type="text" name="telephone" maxlength="30" size="30" placeholder="Number">
<input type="submit" value="Call Me" id="submit">
</form>
And the php file that is referenced:
<?php
if(isset($_POST['email'])) {
$email_to = "sam.skirrow#gmail.com";
$email_subject = "AAO - Someone wants you to call them back";
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['telephone'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$telephone = $_POST['telephone']; // not 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(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last 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 = "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 .= "Telephone: ".clean_string($telephone)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($email_to, $email_subject, $email_message, $headers)) {
header('location: ../success.php');
} else {
header('location: ../fail.php');
}
}
?>
can anyone see why this might be happening??
Your issue is with this line:
if(isset($_POST['email'])) {
Your form has no input with the name email therefore this test will never be true and the entire script is inside this condition. You are submitting $_POST['first_name'] or $_POST['telephone'] you should check if one of those two isset.

How to clear php form after submission

I am trying to clear up the form after submission. I am not sure what I am missing. I will attach the PHP code below. This is my first time coding a contact form or anything using PHP, so try to bear with me. Thank you guys in advance!
Sorry thought I had attached the code.
<?php
if(isset($_POST['email'])) {
// EMAIL AND SUBJECT
$email_to = "#####.com";
$email_subject = "New form submission";
function died($error) {
// your error code can go here
echo "<span style='color: red;' /><center> We're sorry, but there's errors found with the form you submitted.<br /><br /></center></span>";
echo $error."<br /><br />";
echo '<span style="color: red;" /><center>Please go back and fix these errors.<br /><br /></center></span>';
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.</center></span>');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // 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 .= '<span style="color: red;" /><center>The Email Address you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[0-9.-]+$/";
if(!preg_match($string_exp,$telephone) > 10) {
$error_message .= '<span style="color: red;" /><center>The Telephone number you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= '<span style="color: red;" /><center>The First Name you entered does not appear to be valid.<br /></center></span>';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= '<span style="color: red;" /><center>The Last Name you entered does not appear to be valid.<br /></center></span>';
}
if(strlen($message) < 10) {
$error_message .= '<span style="color: red;" /><center>The Message you typed seems to be too short, add more words, please.</center></span><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 .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($message)."\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);
?>
<!-- The success html below -->
<span style="color: green;"><center>Thank you for contacting us. We will be in touch with you very soon.</center></span>
<?php
}
die();
?>
All you need to do is reload the form like you did originally. I generally create a function that displays the form. When the first part of the script detects whether the form was submitted. If it was not, then you display the form. If it was, then you process the input THEN display the form.
Here's some pseudo-ish code to help out.
<?php
function display_form () {
// display your form here
// For my sample, I assume a clear button and a submit button
}
if (isset($_POST['submit'])) { // Submit button was clicked
// put your form processing code here
} else { // Form was not submitted or 'clear' was clicked
display_form();
}
?>
You could have one page with the form and all the information, like contact.php, but submit the data through the form to another page, like,
Then on process_page.php, once you have processed the data, redirect the user back to the original page with query string attached to it, like
header('Location: contact.php?status=mail%20sent%20successfully');
and display the query string through echo $_GET['status'] or create alternative message / layout based on that status. Don't forget to handle cases where $_GET is set, but empty, too!
This is a bit nicer, because it separates your processing logic from the interactive part of the website.

Categories