0% found this document useful (0 votes)
96 views7 pages

Create A Contact Form in PHP Source Code

Source Code of Contact Form in PHP

Uploaded by

wasim ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views7 pages

Create A Contact Form in PHP Source Code

Source Code of Contact Form in PHP

Uploaded by

wasim ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

How to Create a Contact Form in PHP

Source Code
--------------------------------------------------

Have a look at the contact form in the live demo to understand how it works and looks.
For this tutorial you will only need to create just one file. We will call it index.php .
Now that you have created the file, we are ready to code it. First, we will add the HTML Form to
it:
For this tutorial you will only need to create just one file. We will call it index.php .
Now that you have created the file, we are ready to code it. First, we will add the HTML Form to
it:

<form action="index.php" method="post">


<label for="Name">Name</label>
<input type="text" name="Name" id="Name" />
<label for="Subject">Subject</label>
<input type="text" name="Subject" id="Subject" />
<label for="Email">Email</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message</label>
<textarea id="Message" name="Message"></textarea>
<input type="hidden" name="Flag" value="1"/>
<input type="submit" />
</form>

The above form will post back the data to our index.php file which is the main file for our
contact form. It will use the POST method. The form contains 4 fields namely Name, Subject,
Email and Message. I have also added a hidden input field named Flag which we will use to
check if the user has submitted the form.
Now, you will need to add the following PHP code at the beginning of the index.php file:

// Will Be Used To Display Error Messages To Users


$err_msg = "";
// Your Email Address
$admin_email = "[email protected]";
// Check If The Uer Has Submitted The Form
if( isset($_POST['Flag']) && !empty($_POST['Flag']) && $_POST['Flag'] == 1 )
{
if( isset( $_POST['Name'] ) && !empty( $_POST['Name'] ) && preg_match( '/[A-Zaz\s]+/', $_POST['Name'] ) )
{
// The User Has Entered A Name. And The Name Contains Only Alphabets And
Spaces
$name = $_POST['Name'];
}
elseif( !isset( $_POST['Name'] ) || empty( $_POST['Name'] ) )
{
// The Name Input Field Is Empty
$err_msg = "Please Enter A Name";
}
elseif( !preg_match( '/[A-Za-z\s]+/', $_POST['Name'] ) )
{
$err_msg = "Name Can Only Contain Letters And Spaces";
}
if( isset( $_POST['Subject'] ) && !empty( $_POST['Subject'] ) && preg_match( '/[AZa-z0-9\s]+/', $_POST['Subject'] ) )
{
// The User Has Entered A Valid Subject.
$subject = "From Demo Contact Form: ".$_POST['Subject'];
}

elseif( !isset( $_POST['Subject'] ) || empty( $_POST['Subject'] ) )


{
// The Subject Input Field Is Empty
$err_msg = "Please Enter A Subject";
}
elseif( !preg_match( '/[A-Za-z0-9\s]+/', $_POST['Subject'] ) )
{
$err_msg = "Subject Can Only Contain Letters, numbers And Spaces";
}
if( isset( $_POST['Email'] ) && !empty( $_POST['Email'] ) && preg_match( '/[a-zAZ0-9_\.]+@[a-zA-Z0-9]+.[a-zA-Z]+/', $_POST['Email'] ) )
{
// The User Has Entered A Valid Email.
$email = $_POST['Email'];
}
elseif( !isset( $_POST['Email'] ) || empty( $_POST['Email'] ) )
{
// The Email Input Field Is Empty
$err_msg = "Please Enter A Email";
}
elseif( !preg_match( '/[a-zA-Z0-9_\.]+@[a-zA-Z0-9]+.[a-zA-Z]+/',
$_POST['Email'] ) )
{
$err_msg = "Please Enter A Valid Email";
}
if( isset( $_POST['Message'] ) && !empty( $_POST['Message'] ) )
{
// The User Has Entered A Message.
$message = $_POST['Message'];
}
elseif( !isset( $_POST['Message'] ) || empty( $_POST['Message'] ) )
{
// The Message Input Field Is Empty
$err_msg = "Please Enter A Message";
}
if( $err_msg == "" )
{
// There Are No Errors in The User Input

// We Can Now Send The Email


// Set From Header. Will Look Like This: From: Mohit
Gangrade<[email protected]>
$headers = 'From: ' . $name . '<'. $email .'>';
mail($admin_email, $subject, $message, $headers);
}
}
The Code is well commented but I will break it down into small parts to explain it.
Part 1:

// Will Be Used To Display Error Messages To Users


$err_msg = "";
// Your Email Address
$admin_email = "[email protected]";

// Check If The Uer Has Submitted The Form


if( isset($_POST['Flag']) && !empty($_POST['Flag']) && $_POST['Flag'] == 1 )
{
}
First, I created a variable named $err_msg which we will use to store error messages and display
them to the users. The second variable named $admin_email is the email we will send emails to.
After that I am checking if our hidden input named Flag is set and is not empty. And if it equals
to 1 then it means that the user has submitted the form.
Now in the part 2 below, I will explain what will happen when the user submits the form.

Part 2
if( isset( $_POST['Name'] ) && !empty( $_POST['Name'] ) && preg_match( '/[A-Za-z\s]
+/', $_POST['Name'] ) )
{

// The User Has Entered A Name. And The Name Contains Only Alphabets And
Spaces
$name = $_POST['Name'];
}
elseif( !isset( $_POST['Name'] ) || empty( $_POST['Name'] ) )
{
// The Name Input Field Is Empty
$err_msg = "Please Enter A Name";
}
elseif( !preg_match( '/[A-Za-z\s]+/', $_POST['Name'] ) )
{
$err_msg = "Name Can Only Contain Letters And Spaces";
}

if( isset( $_POST['Subject'] ) && !empty( $_POST['Subject'] ) && preg_match( '/[AZa-z0-9\s]+/', $_POST['Subject'] ) )
{
// The User Has Entered A Valid Subject.
$subject = "From Demo Contact Form: ".$_POST['Subject'];
}
elseif( !isset( $_POST['Subject'] ) || empty( $_POST['Subject'] ) )
{
// The Subject Input Field Is Empty
$err_msg = "Please Enter A Subject";
}
elseif( !preg_match( '/[A-Za-z0-9\s]+/', $_POST['Subject'] ) )
{
$err_msg = "Subject Can Only Contain Letters, numbers And Spaces";
}

if( isset( $_POST['Email'] ) && !empty( $_POST['Email'] ) && preg_match( '/[a-zAZ0-9_\.]+@[a-zA-Z0-9]+.[a-zA-Z]+/', $_POST['Email'] ) )
{
// The User Has Entered A Valid Email.
$email = $_POST['Email'];

}
elseif( !isset( $_POST['Email'] ) || empty( $_POST['Email'] ) )
{
// The Email Input Field Is Empty
$err_msg = "Please Enter A Email";
}
elseif( !preg_match( '/[a-zA-Z0-9_\.]+@[a-zA-Z0-9]+.[a-zA-Z]+/',
$_POST['Email'] ) )
{
$err_msg = "Please Enter A Valid Email";
}

if( isset( $_POST['Message'] ) && !empty( $_POST['Message'] ) )


{
// The User Has Entered A Message.
$message = $_POST['Message'];
}
elseif( !isset( $_POST['Message'] ) || empty( $_POST['Message'] ) )
{
// The Message Input Field Is Empty
$err_msg = "Please Enter A Message";
}
In the above code I am only checking if the user has filled all our input fields correctly or not. I
am validating the fields like this:
Name: I am first using 2 PHP functions, isset and empty to check if the user has entered a Name
or not. I am also using basic regular expressions to check if the Name only contains letters and
spaces.
Subject: Again, I am checking if the user has entered a subject or not. And I am also using
regular expressions to check if the subject contains only letters, numbers and spaces.
Email: This time I am using a somewhat complex regular expression to validate email.
Message: For the message, I am only checking if it is not empty.
If there is any error in the user input then I changing the value of$err_msg to the error message
text. Now that we have validated the user input we need to send an email.

Part 3:

if( $err_msg == "" )


{
// There Are No Errors in The User Input
// We Can Now Send The Email
// Set From Header. Will Look Like This: From: Mohit
Gangrade<[email protected]>
$headers = 'From: ' . $name . '<'. $email .'>';
// Wrap the message if it exceeds 70 characters per line.
$message = wordwrap($message, 70, "\r\n");
mail($admin_email, $subject, $message, $headers);
}
Now, in this part I am checking if our variable $err_msg is empty. And if it is empty then it
means that there are no errors in the user input and we can send the mail.
Inside the if statement I created a new variable called $headers. We will use this variable to add
additional headers to the mail function. For this tutorial, I am only changing the From header.
Changing the From header to the name and email of the user will allow you to reply directly.
After that I am using the word_wrap to wrap the Messages. It is necessary to wrap the message.
Now that we have all the parameters of the mail function ready. I am just making a call to
the mail function with all the parameters.
The PHP code for sending mail and validating user input is complete. But you will need to
display the error messages to the user. Add the below code right before the opening form tag:

<?php
if($err_msg != "")
echo "<div id='ErrorMessage'>$err_msg</div>";
?>
The above code will check if the $err_msg is empty or not. If it isnt empty then it will output
the error to the user.

You might also like