PHP Contact Form

Last Updated on March 5, 2023 by Roshan Parihar

In this post, learn about the PHP contact form and the script for validation and send emails with form data. You will also get PHP code to insert data into the database and display it on your website pages.

If you are running an online business, a contact form can be the best solution for you to collect user queries from your website. You can add contact forms to your website contact page.

PHP Contact Form

Bonus Tip

Looking to create contact forms without coding? Create beautiful looking contact forms with JotForm.

Each time when your users want to contact you, they will visit to contact us page and fill out the contact form to send their queries. After collecting queries from your users, you can start further communication to convert them into regular customers.

You can easily create a contact form and send data in emails using PHP. The contact form sends emails only after getting valid entries from users. It also inserts data into the database after all the user’s input data are correct and valid.

So, let’s get started.

PHP Contact Form Appearance

The contact form should contain all the necessary fields to easily collect users’ data with queries. It also contains an email or phone number to contact the users for communication.

Longer size contact form is difficult for users to fill in data. That way, to make the contact form as simple as possible I have added only three form fields that are given below:-

  • Name
  • Email
  • Message

The above fields are enough to easily collect queries and contact your users with an answer to their queries. The HTML code for the contact form is given below.

The appearance of the contact is shown below. The interface not looks good and to make it beautiful, you have to add some CSS for styling

contact-form-in-html PHP Contact Form

To add styling to your contact form, you have to add some CSS given below. It styles all the input fields, text area, and the submit button. You can change the submit button color as per your website color.

Add Style to Contact Form

After adding styling to your contact form, your form appearance will look like the image below. The interface looks good to add to your website and display to your users. It is responsive to easily display in any size device like desktop, laptop, tablet, and mobile.

contact-form-in-html-css

Now, let’s add some validation to your contact form to collect valid data from users. After that, you can use the code below to send an email that contains the users entered data.

PHP Contact Form Add Validation and Send Data in Emails

HTML input fields come with the default attribute required that you can add to your input fields to make it required. The only validation here which is required is email format.

The below example code matches the email regex expression with the user input email address to find the valid email address. If the email address is not valid, it displays error messages. When the user enters a valid email address, it executes the next PHP code to send the email address.

Now, next is to insert the data into the database and display it on a page. Let’s do this in the next section.

Insert Form Data Into Database and Display in a Page

After you insert the data into the database, you can display the data somewhere on your website page. It is useful when you want to find the user’s email address to start communication with them in the future.

You have to first create a database in your PHPMyadmin.

login to PHPMyAdmin

phpmyadmin-login PHP Contact Form

After that, open the ‘SQL’ tab there.

Open the ‘SQL’ tab of PHPMyAdmin

phpmyadmin-dasboard-click-sql-tab

Create a database to insert PHP contact form data

Now, copy the below code and add it to the input area given in the ‘SQL’ tab. After that, press the ‘Go’ button to execute the below SQL code and create the database with the name ‘contact_form’.

phpmyadmin-add-code-click-go-to-insert-db PHP Contact Form

It also creates the table ‘contacts’ with the creation of the database ‘contact_form’.

Connect to Your Database Using PHP

After you create the database, you can connect to the database using the PHP code given below. The code requires the server name, the username of the database, and the password of the database user.

The code also checks the connection to show a success message when the connection is made successfully.

Insert Contact Form Data Into the Database

To insert the contact form data into your created database, you have to use the PHP code given below. The code for the PHP contact form inserts the data into the database and displays the success message after insertion.

Now, you have successfully sent the email and inserted the form data into the database. It’s time to display the data on your website page to make it easier for the website owner to keep a record of users.

Display Inserted Data in a Table

For the below PHP code first, select the table ‘contacts’ and check whether it contains user records or not. After that, it displays all the data in a table using a loop in PHP.

You will get the user’s data in the table format on your website. To make your table looks better on your website, you can also add some style given below.

phpmyadmin-add-code-click-go-to-insert-db

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.