Easy Breeze in Web Design

How to Create Contact Form in PHP and Email Form Results

Forms are very useful in getting information from your website readers. Your form can be a survey form, a contact contact us form or even a multiple choice question and answer. To get the answers from the online forms which your readers submitted, you can receive the results via email. In our example, we will teach you how to create a contact form in PHP and email form results.

Basically, you will need an HTML script to create the form and PHP script that will catch the results from the forms and send it to your email. In our example, we will use a contact form that will send the results to email.

Below is the HTML code for contact form. You may edit this depending on fields that you need for your website. The code “send_email.php” refers to the file name of the php script that you will use. Make sure that your PHP file will be the same with this one.

<form action=”send_email.php” method=”post” name=”contactform”>
<table width=”557″>
<tbody>
<tr>
<td valign=”top”>
<label for=”first_name”>First Name *</label>
</td>
<td valign=”top”>
<input type=”text” name=”first_name” size=”30″ maxlength=”50″/>
</td>
</tr>
<tr>
<td valign=”top”>
<label for=”last_name”>Last Name *</label>
</td>
<td valign=”top”>
<input type=”text” name=”last_name” size=”30″ maxlength=”50″/>
</td>
</tr>
<tr>
<td valign=”top”>
<label for=”email”>Email Address *</label>
</td>
<td valign=”top”>
<input type=”text” name=”email” size=”30″ maxlength=”80″/>
</td>
</tr>
<tr>
<td valign=”top”>
Your age
</td>
<td valign=”top”>
<input type=”text” name=”age” size=”30″ maxlength=”30″/>
</td>
</tr>
<tr>
<td valign=”top”>
Message
</td>
<td valign=”top”>
<textarea style=”width: 374px; height: 130px;” name=”experience” rows=”6″ cols=”25″></textarea>
</td>
</tr>
<tr>
<td style=”text-align: center;” colspan=”2″>
<input type=”submit” value=”Submit”/>
</td>
</tr>
</tbody>
</table>
</form>

The output of the code above will be:

Your age
Message

 

So that you can receive the results to your email, you will need the PHP script below. Open a notepad and save it using the file name “send_email.php”. You may change this provided that you also change the file name indicated in the HTML code. Make sure that the file name extension .php remains

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

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = “enteryouremailhere@email.com”;
$email_subject = “Contact form results”;

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['age']) ||
!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
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$age = $_POST['age']; // not required
$message = $_POST['message']; // required
$chk = $_POST['chk ']; // 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($message) < 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 \n”;
$email_message .= “Last Name: “.clean_string($last_name).”\n \n”;
$email_message .= “Email: “.clean_string($email_from).”\n \n”;
$email_message .= “age: “.clean_string($age).”\n \n”;
$email_message .= “message: “.clean_string($message).”\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 –>
<h1>Message Sent!</h1>
<p>Thank you your interest in joining us! We will respond to you as soon as we have evaluated your application. Further instructions will be sent to your email. Please check your email regularly.</p>
<p>Have a great day ahead of you!</p>
<?php
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Designed by Affordable Web Design Guide | Copyright 2011