PHP Contact form help

Hi Guys,

I’m trying to put together a contact form on my website using a free PHP form I downloaded and need some assistance. I don’t know the first thing about PHP and need some assistance as far as redirecting the page to a fail/success page.

here is the PHP Code:

[php]<?php
if(isset($_POST[‘email’])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = “Contact Form for Instill Solutions”;

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.

”;
echo $error.”

”;
echo “Please go back and fix these errors.

”;
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.
’;
}
$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.
’;
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= ‘The Last Name you entered does not appear to be valid.
’;
}
if(strlen($comments) < 2) {
$error_message .= ‘The Comments you entered do not appear to be valid.
’;
}
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);
?>

Thank you for contacting us. We will be in touch with you very soon.

<?php } ?>[/php]

So what I want to happen is to have the errors go to certain pages or be able to have the error “posted” to that certain page.

If form submitted correctly go to success.html
If it’s not, go to formerror.html and be able to display the error.

Right now it just goes to blank pages for all that. Any help would be greatly appreciated.

I’m just trying to get the pages to have all the content:

http://www.mysite.com/thankyou.html

I only put the actual form on the home page:

http://www.mysite.com

Simply use the page redirection PHP code. It is simple to use. In your validation code when you want to send it to another page use:
[php]

<?PHP header('Location: http://www.example.com/'); ?>

[/php]

Note, this can be used with local pages inside your website or an external one like above. To use a page already in your website it would be something like:
[php]
header(‘Location: InvalidForm.php’);
[/php]

You can create a “Thank-You for sending your information” page and another “Invalid data entered” page and use your PHP if’s to tell which to go to… Hope that helps…

thats perfect, thank you very much!!

I was able to get the success redirect to work correctly. where exactly would I place the code for the validation ones? Where do I put it in the PHP code and where would I put it in the HTML page?

Here is my success page:

www.instillsolutions.com/thankyou.html

so I would want the error code to automaticaly fill in in the thank you for contacting us.

Thank you again for your help

Well, there are lots of ways to do this. Obviously, you are posting from a contact-us form into your PHP code. Inside of the PHP code that you posted here, you would have to create a variable to pass onto your THANK-YOU page. Your thank-you page would become a “Incorrect-Info” page or whatever. Basically, the text of the message would be passed to the page and then displayed.

One way to do this is with SESSION variables. It is easy to use and users do not see the data being passed. You would change the page you posted into a PHP page. (.PHP not .html) The data for the message would be inside the code you posted. So, at the start of that page add “session_start();” .
This starts the session. All pages using a session variable will need this as it’s first PHP code line.
Next, create the variable to be passed in two places of the code. If the email info was validated and
an email was sent, then use something like this:
[php]
$_SESSION[‘Message’]= “Thank you for contacting Instill Solutions.
One of our sales representatives will be contacting you shortly to go over your Internet marketing needs.”
[/php]
And, if the validation fails, you would put the corresponding error message. Let’s say they forgot to include their phone number, you would do this:
[php]
$_SESSION[‘Message’]= “Thank you for contacting Instill Solutions.
But, you did not enter your phone number. We need this so our sales representatives can contact you.
Please go back and enter your number.”
[/php]
Yes, you would have to create a message for each possible error. In your validation if-elseif section, the first error would redirect so only one error would post at a time. You, could create an array of errors and have the display page print them all. Either way, on the display page that you posted which would now be a PHP page, you would use something like this to display the error or success message:
[php]

<?PHP session_start(); echo $_SESSION['Message']; ?>

[/php]
It will display the message that was passed to it. This code would replace the actual thank-you message you currently have in text.

Hope that all made sense… Good luck…

I think I’m getting this, just need a little clarification. On the “HTML” page, that we will now save as .php. I can keep all of my current html code for the design, and in the table where I want the message displayed I will put in:

At the beginning of the table:

[php]session_start();[/php]

Then:

[php]$_SESSION[‘Message’]=[/php]

This will automatically pull the message out of the PHP script, did I understand that correctly? Or do I have to create separate messages in this with quotes after the =?

On the PHP Script. Where exactly do I put:

[php]<?PHP
session_start();
echo $_SESSION[‘Message’];
?>[/php]

Here is the script currently. Sorry for all the questions, I just never used PHP before. I seem to have an understanding of what each line is doing, but very very basic understanding.

[php]<?php
if(isset($_POST[‘email’])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Contact Form for Instill Solutions";
 
 
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.
’;
}
$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.
’;
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= ‘The Last Name you entered does not appear to be valid.
’;
}
if(strlen($comments) < 2) {
$error_message .= ‘The Comments you entered do not appear to be valid.
’;
}
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: http://www.instillsolutions.com/thankyou.html’);

?>

Thank you for contacting us. We will be in touch with you very soon.

<?php } ?>[/php]

You almost have the idea…

I your code:
[php]
if(strlen($error_message) > 0) {
died($error_message);
}
[/php]
Where you kill the page if they do not do what you want correctly, you would change this to pass on the message to your nice “thank-you” page. Something like this instead:
[php]
if(strlen($error_message) > 0) {
// Set the session variable to be used in your main page… (Assumes session_start(); earlier)
$_SESSION[‘errormessage’] = $error_message;
// Redirect this code to the thank-you page with the error in place…
header(“Location: http://www.mysite.com/thankyou.php”);
// OR, redirect to the page in this manner… (Does not have to go out to the site…)
header(“Location: thankyou.php”); //(Assumes both pages are in same site folder…)
}
[/php]
This code sends your error-message inside a session variable which can be used in your thankyou page. The page has to be renamed to thankyou.php NOT thankyou.html so the PHP code will work. AND, you have to change were you currently have the thank you TEXT. Instead, replace the actual text with this:
[php]

<?PHP session_start(); echo $_SESSION['errormessage']; ?>

[/php]
Simple. The text is displayed. Now, since you have both your “THANK-YOU” and “ERROR-MESSAGE” both coming from the same PHP code and going to the same shell html, you may want it to not be separated. So, the errormessage could be just message. In that way, you can use the thankyou.html as a generic message page, call it something like sitemessage.php. Or whatever works for you. Then, whenever you need to display a note of an error, or thank you or welcome, whatever, just send it there…

Hope all that makes sense… Good luck…

So I think I got it but it seems to taking me to the wrong page. When I hit the submit button on the form it goes to:

www.instillsolutions.com/contact_form_email.php

It doesn’t seem to be redirecting to the /thankyou.php

I replaced the tag:

[php]if(strlen($error_message) > 0) {
died($error_message);
}[/php]

with:

[php] if(strlen($error_message) > 0) {
$_SESSION[‘errormessage’] = $error_message;
header(“Location: http://www.instillsolutions.com/thankyou.php”); [/php]

Any idea?

here is the full PHP code:

<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "[email protected]"; $email_subject = "Contact Form for Instill Solutions"; 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.

"; echo $error."

"; echo "Please go back and fix these errors.

"; 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.
'; } $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.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { $_SESSION['errormessage'] = $error_message; header("Location: http://www.instillsolutions.com/thankyou.php"); } } $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); ?>

Thank you for contacting us. We will be in touch with you very soon.

<?php } ?>

Sorry, I haven’t been well today. I saw your post.

Did you close this with a bracket? }

if(strlen($error_message) > 0) {
$_SESSION[‘errormessage’] = $error_message;
header(“Location: http://www.instillsolutions.com/thankyou.php”);

It needs to be closed… Also, was session_start(); at the beginning of the PHP code?

That seems go have worked by fixing the close out bracket and putting in the session start at the beginning. Thank you so much!

just one last fix that I can’t seem to figure out. all the error messages are working great, however I can’t get the success message to print. I’m assuming it’s because of the way the code is set up for success vs error?

[php] if(strlen($error_message) > 0) {
$_SESSION[‘errormessage’] = $error_message;
header(“Location: http://www.instillsolutions.com/thankyou.php”);
}
$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);

?>

Thank you for contacting us. We will be in touch with you very soon.

<?php[/php] I'm just getting the "Thank you for contacting us. We will be in touch with you very soon" on a blank page. Can I get that to post in the same thankyou.php page? or another page and direct to it? I already have a thankyou.html made up, just not sure how to use the header tag properly. Again can't thank you enough for your help!

Sure, this is because you have it this way…
[php]
if(strlen($error_message) > 0) {
$_SESSION[‘errormessage’] = $error_message;
header(“Location: http://www.instillsolutions.com/thankyou.php”);
}
[/php]
It should be more like this:
[php]
if(strlen($error_message) > 0) {
$_SESSION[‘errormessage’] = $error_message;
header(“Location: http://www.instillsolutions.com/thankyou.php”);
} else {
//your email setup and sending code here…
header(“Location: http://www.instillsolutions.com/thankyou.php”);
} //end of if-else with both success message and errormessage going to the same output page…
[/php]
***One note: I would rename the message from ‘errormessage’ to just ‘message’… Sense you are sending both errors and success messages to the same page. Small change, but, in the future it will be easier to debug realizing that it is not just an error message. Perhaps ‘resultsmessage’ might be better?
It’s these simple things that help in the long run… Just FYI… Good luck…

So one last thing… I was able to figure the success message out just pasting the HTML of the page I want to display, I know it’s not the “correct” thing to do but for some reason the “else” just kept displaying wrong e-mail error.

Everything works with one exception. Errors all show up, success page loads, BUT I get an e-mail no matter if there is an error or not. What do I have to put in the code so that if there IS an error no e-mail gets sent till they fix it?

[php]<?php
session_start();

if(isset($_POST[‘email’])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Contact Form: Instill Solutions";
 
 
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.
’;
}
$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.
’;
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= ‘The Company Name you entered does not appear to be valid.
’;
}
if(strlen($comments) < 2) {
$error_message .= ‘The Comments you entered do not appear to be valid.
’;
}
if(strlen($error_message) > 0) {
$_SESSION[‘errormessage’] = $error_message;
header(“Location: http://www.instillsolutions.com/thankyou.php”);
}
$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);

?>[/php]

Hmmm… Well, first ALL functions should be outside any IF’s. Functions are always assigned at the beginning of the code unless they are “local” to one routine. Your functions were not used in your posted code, so I removed them.

Also, your check for errors, and have some “die” code and not IF-ELSE as I mentioned before. So your code does some vailidation, but, does not create an if-bad display bad, if-good display good AND email…
Instead, it does a if-bad display bad, if good display good THEN no matter what, emails…

So, it really needs to be if-bad, set up bad message… if-good, set up good message AND email
Like:
message=""
if validation(of stuff) = bad then message=“bad inputs”
if validation(of stuff) = good then message=“nice inputs”, email
and nothing else…

I rewrote your code to do this, here is a version that should work… (I did not test it)
But, since you never actually call a “die()” as you send the message (error or not) to the next page, you do not need to ever Kill your code… Deleted that function… (I did keep the Go back and fix part!)
The line: if(isset($_POST[‘email’])) { is NOT needed… The email form is always posted if it gets here…
Without seeing your HTML-FORM, I am guessing. But, a form has an action and that action calls the PHP code, which is all you posted… So, assuming the form is called “email”, you do not need to check if it is set. If you have several parts of the form and need to check for “email” being the part posted, then, yes, you would need that if(isset… I will assume NOT… So, either add it around this code following or just use this code. (if that did not make sense, then post your form and I will explain…
So, this would be the way, I would do it…, hope it makes sense…
[php]

<?php session_start(); // Now, validate information. If bad say so, if not email... First set up a blank message... $error_message = ""; // First, load the form's fields for validation uses... $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email_from = $_POST['email']; $telephone = $_POST['telephone']; $comments = $_POST['comments']; // Validate any expected data exists (Do before setting up email information...) if(!isset($_POST['first_name']) $error_message .= "First name is missing!

"; if(!isset($_POST['last_name']) $error_message .= "Last name is missing!

"; if(!isset($_POST['email']) $error_message .= "Email address is missing!

"; if(!isset($_POST['telephone']) { $error_message .= "Telephone number is missing!

"; // Now validate some of the text further... $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.
'; $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.
'; if(!preg_match($string_exp,$last_name)) $error_message .= 'The Company Name you entered does not appear to be valid.
'; if(strlen($comments) < 2) $error_message .= 'The Comments you entered do not appear to be valid.
'; // Posted input text data has been validated, now handle the errors or send the email... if(errormessage!="") { // Form's imputed data has been validated with no errors, now send email... $email_to = "[email protected]"; $email_subject = "Contact Form: Instill Solutions"; $email_message = "Form details below.\n\n"; $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); $errormessage="Congratulations! Your information has been forwarded to our offices!"; }else{ // Form's imputed data is bad, so do NOT send email and explain... $error_message .= "Errors were found in your form submission!

Please go back and fix these errors.

"; } //Now all data has been validated and errors are ready for display or email was sent, now say so either way... $_SESSION['errormessage'] = $error_message; header("Location: http://www.instillsolutions.com/thankyou.php"); ?>

[/php]
**** Several changes in your layout of your validation and email setup/sending… But, all your code, just rearranged!!!
SOOOOOO, you had, it, just slightly not in the right order… So pat on your back… Good luck…

Hello, I am new to the forum.

Sponsor our Newsletter | Privacy Policy | Terms of Service