How To Create PHP Contact us Form for Website

PHP Contact form

In this post we will learn How To Create PHP Contact Form for website and How To Add PHP Contact Form To Our Website.

we are know contact form of websites led user to communicate with website owners. This shows a genuine or loyal behavior of an organization towards its customers

the contact form needs to have two parts: the client side ‘front end’ of the form and the server side ‘back end’ of the form. The client side of the form is coded in HTML plus some style CSS and JavaScript.

We will first create a simple contact form with 4 fields – Email address, Name, Website, Massage. I will use a table to align the 4 fields and the Send button. Create a new file and paste the code below in it. Save it as mail.php and upload it to your web server in public Folder. Now, you have a web page with a simple contact form on it.

<form class="form" action="mail.php" method="post"> 
    <p class="name"> 
        <input type="text" name="name" id="name" /> 
        <label for="name">Name</label> 
    <p class="email"> 
        <input type="text" name="email" id="email" /> 
        <label for="email">E-mail</label> 
    <p class="web"> 
        <input type="text" name="web" id="web" /> 
        <label for="web">Website</label> 
    <p class="massage"> 
 <textarea name="massage" id="massage"></textarea> 
    <p class="submit"> 
        <input type="submit" value="Send" /> 

this is front side basic contact form you can make this contact form more stylish using css

input, textarea { 
    padding: 9px; 
    border: solid 1px #E5E5E5; 
    outline: 0; 
    font: normal 13px/100% Verdana, Tahoma, sans-serif; 
    width: 200px; 
    background: #FFFFFF url('bg_form.png') left top repeat-x; 
    background: -webkit-gradient(linear, left top, left 25, from(#FFFFFF), color-stop(4%, #EEEEEE), to(#FFFFFF)); 
    background: -moz-linear-gradient(top, #FFFFFF, #EEEEEE 1px, #FFFFFF 25px); 
    box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px; 
    -moz-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px; 
    -webkit-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px; 
textarea { 
    width: 400px; 
    max-width: 400px; 
    height: 150px; 
    line-height: 150%; 
input:hover, textarea:hover, 
input:focus, textarea:focus { 
    border-color: #C9C9C9; 
    -webkit-box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 8px; 
.form label { 
    margin-left: 10px; 
    color: #999999; 
.submit input { 
    width: auto; 
    padding: 9px 15px; 
    background: #617798; 
    border: 0; 
    font-size: 14px; 
    color: #FFFFFF; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 

Now see what’s been added Take a look at the opening <form>

tag. We’ve added in this form two new attributes; method and action.

The method=”post” attribute provides the PHP script with a method of extracting the values from the form, so the script can then do what it wants with those values. The action=”mail.php” attribute simply tells us the location of the PHP script; in this case, because it’s only a few lines of code, we’re going to insert it just above the form in our mail.php file. So when the form is submitted, it will go to mail.php and find the script.

Take another look at the HTML above and you will see that each of the form elements have the name attribute. The PHP uses this attribute to extract the data from each of the form elements. Let’s look at how it does this

$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['phone'];
$massage = $_POST['call'];

$message = $_POST['message'];
$formcontent=" From: $name n website: $website n Message: $message";
$recipient = "";
$subject = " Contact form";
$mailheader = "From: $email rn";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Thank you we will contact you as soon as possible  Return Home</a>";

If you’ve never used PHP before, it might look a bit confusing – but it’s actually really simple. The $_POST part is a built in PHP function which is used to collect data from a form that uses method=”post” like ours does. The part that immediately follows this function, [‘name’] must correspond to the name attribute from the form input; in the first instance, name=”name”!

So, this will collect whatever data has been entered into the name field – now we need something to store that data in. This is where variables come into play. In this case, $name is the variable that we will use to store the data from the name field. We can then do the same for the email and message fields. Simple!

now save the your php file and give the name mail.php and host it in public folder you can try free web hosting service for test your php contact form



  1. Venta

    Hello my loved one! I want to say that this article is awesome, nice written and come with approximately all important infos. I would like to see extra posts like this .


Leave a Reply