How to Create Custom PHP Contact Forms

The contact form is a really essential portion of any website. Because it gets leads for your business, hence we can say that it is a source of your business. We can find a contact form in any website we pick. [Best PHP Contacts Forms]

In this article, we will see how we can create a simple contact form for your website using PHP as a server-side programming language. It’s correct that everyone needs different fields in their contact form but what we will create is kind of base contact form which you can utilize/customize based on need.

The process of generating leads from contact form is same regardless of a website, type of form and numbers of fields in a form. This process includes collect data from a user and sends those to the server. The server will have code which send these data to a predefined email address so that can be used to process lead further. [PHP Templates]

In this article, we will see how we can create a simple PHP contact form with essential fields which are a name, email address, and message.

Building the Form

We will need two files in order to complete this process. contact.html which holds the HTML structure of our form and start the lead generation process. Next is mail.php, which will send the provided details via email.

At this stage, it is assumed that you already have your HTML page and not its time to add form in it. So for that very first thing is to add form tag which are as below:

We need to make sure to add all the required fields between this form tag only. Next step is to add method and actions to our form. The action is the page URL where our form data will be submitted and method defines using which method our data is being submitted so we can process data accordingly.

At this stage, our form is ready to post data but before that we need to make sure that we have added required fields in our form. We will add Name, Email and Message to this form.

Here we go, our form is not ready with all the fields. Now when any user fills the information and submits data we can get all those data in mail.php. In next section, we will see how to send the email to the email address.

Sending Email

As we have used POST as a method of our form, we will receive all the posted data in a $_POST array variable. Also, we need to keep in mind that $_POST array will have keys set as the name of our HTML elements in our form. So a value of element having a name as an ’email’ can be retrieved using $_POST[’email’]. Content for mail.php can be as mentioned in below code block.

At this stage, you are done with your code and you are ready to deploy this code for your website. But what if you want to modify your thank you a message?

Don’t worry it is not that much complex, what you need to amend is just a below line of code as per your need and you are done with this.

I know this is very simple but its much need article for startup developer. Hope you find this helpful.

by Irshad Shaik

You may also like

Read More Articles about

Tutorials