Simple PHP Contact Form

Follow the tutorial below to get the script you need.
For more advanced features sign up for free and try our drag & drop form builder.

Simple PHP contact form with process on same page

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 and 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.

To download the simple PHP contact form script, click here and "save as" the file, or download the code below. You can also check our tutorial about jQuery Contact forms with Ajax and PHP.

The PHP contact form script

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"/>
else                /* send the submitted data */
    if (($name=="")||($email=="")||($message==""))
		echo "All fields are required, please fill <a href=\"\">the form</a> again.";
	    $from="From: $name<$email>\r\nReturn-path: $email";
        $subject="Message sent using your contact form";
		mail("", $subject, $message, $from);
		echo "Email sent!";

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 either in the same file or in two different ones, as you will see in the example above

Step 2 Copy the simple PHP contact form script from above

We will create a simple PHP contact form, that will require the user to enter his name, email and message (see the image on the lef). Below we will include the PHP contact form code, nicely highlighted, for your easy understanding.

When the visitors open the page with the form (live demo), the HTML code of the form is displayed (highlighted with black). After the users click the Send Email button they are taken to the same page, but a hidden parameter named action is set. This will prevent the script from displaying the form again and from trying 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 shown.

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 (single choices, multiple choices, 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 can add a Captcha image verification. If users mistype their emails, you can 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!

Sign up, get your own PHP contact form and publish it on the web.

You do not need programming skills for this! Just copy-paste!