Simple PHP contact form

Create free online PHP Contact Forms as easy as 1-2-3 using
a simple PHP script or 123ContactForm web form builder.

Free Online PHP Contact Forms

Contact forms are the easiest way to let the visitor contact you, without revealing your email address on the web page, which would surely make your email be added in spam lists. Contact forms can be built with various scripting languages, but the most popular contact forms are PHP contact forms. They are very flexible, can be linked with databases, but PHP is not very easy to learn, especially by beginners in web development. We will show you a short tutorial on how to create a simple PHP contact form.

Simple PHP Form Script with Demo

Simple PHP contact form with process on same page

  • Step 1: Create a single PHP file
    A PHP contact form is built of two parts: the actual contact form (what visitors see) and the script that processes the filled form and sends the email. They can be located in two different files, or they can be located in the same file, as you will see in the example below.

  • Step 2: Copy the simple PHP contact form script from below
    We will create a simple PHP contact form, that will require the user to enter his name, email and message (see the above image). Below we will include the PHP contact form code, nicely highlighted, for your easy understanding. When the visitors opens the page with the form (live demo), the HTML code of the form is displayed (highlighted with black). After the users clicks the Send Email button, the user is taken to the same page, but a hidden parameter named action is set. This will prevent the script from displaying the form again and try to send the email (the code after the first else). The script verifies if all the fields were filled. If at least one was let empty, an error message is showed.

The PHP contact form script

To download the simple PHP contact form script, click here and "save as" the file, or copy the code below.
<?php
$action
=$_REQUEST['action'];
if (
$action=="")    /* display the contact form */
    
{
    
?>
    <form  action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>
    <?php
    

else                
/* send the submitted data */
    
{
    
$name=$_REQUEST['name'];
    
$email=$_REQUEST['email'];
    
$message=$_REQUEST['message'];
    if ((
$name=="")||($email=="")||($message==""))
        {
        echo 
"All fields are required, please fill <a href=\"\">the form</a> again.";
        }
    else{        
        
$from="From: $name<$email>\r\nReturn-path: $email";
        
$subject="Message sent using your contact form";
        
mail("youremail@yoursite.com"$subject$message$from);
        echo 
"Email sent!";
        }
    }  
?>



You can also check our tutorial about jQuery Contact forms with Ajax and PHP.

Get more features by using our drag & drop web form builder


That was it, we created a simple form. If you want to add other fields (radio buttons, checkboxes, textareas, dropdown lists) to the form, you have to add them to the script. If spammers start to automatically fill your form with garbage, you have to add a Captcha image verification. If the users mistype their emails, you have to add form validation. If you want your visitors to send you files with the contact form, you can let them upload files with the PHP contact form.

All these features can be implemented in PHP, but they require advanced knowledge of PHP. An easier solution would be to use 123ContactForm free PHP form generator, which offers all these features and many others. Don't hesitate!
Free Online PHP Contact Form Generator