Contact Form PHP Source Code with MySQL

Last Updated on April 6, 2023 by Roshan Parihar

Contact form PHP source code can be used in your websites to connect with your audience. It also contains the MySQL code to keep a record of your users who want to communicate with you via the form.

It contains the code for HTML structure and design with PHP code to collect users entered data. After that, you can also display the inserted contact form entered data using PHP with MySQL.

Contact Form PHP Source Code with MySQL

Bonus Tip

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

A contact form is the most essential part of websites running an online business. Without a contact form, your audience can’t contact you to get your product. It can help you gain more customers and revenue

In this post, you will get contact form HTML code, design code, and PHP source code to send emails. It also contains the PHP code to insert and display the contact form entries.

So, let’s get started.

Contact Form HTML Structure Source Code

To create the main structure of the contact form, you have to use the HTML source code given below. It contains the HTML <form> tag, <input> tag, and <textarea> tag. The input types are text for the Name field, email for the email field, and submit for form submission.

After you create the HTML structure, you will get the very basic design of the contact form as given below.

contact-form-in-html Contact Form PHP Source Code with MySQL

Add Style to Contact Form with CSS Source Code

To make your contact form design looks beautiful, you have to use the CSS source code given below. The CSS source code beautifies the input fields, textarea field, and submit button. You can modify the CSS code to match the contact form with your brand.

The CSS source code also contains the asterisk mark to display with the label for the input fields that are required for users to fill. It is useful to help your audience and customers know the required fields in a contact form.

After you add the CSS source code, your contact form design becomes beautiful as shown in the image below. It is now ready to display on your website.

contact-form-in-html-css

Let’s add PHP source code to send emails and insert the user’s submitted data into the database. After that, you can display the records on your website.

Contact Form PHP Source Code (For Validation and Send Emails)

This PHP source code first collects all the form input data using the $_POST global variable. After that, it validates the format of the email address. There is no need to manually make fields required as we have already added the required attribute to all input fields.

When the validation of the email address finds it valid, it sends the email using the simple mail() function of PHP. If you want to use the PHPMail SMTP to send emails, you can read our tutorial post on Contact Form Using PHPMailer Gmail SMTP to Send Emails.

The PHP mail() function takes four variables to send emails. These variables are explained below:-

  • $to: it is the email address of the recipient who wants to receive the user’s entered data via email.
  • $subject: The main subject of the email.
  • $message: It is the content of the email that can be the user’s data entered in the contact form.
  • $headers: The headers required to send the emails.
    • From: It can be the email address of the users who fills out the contact form.
    • CC: The other email address that also wants to receive the emails same as the admin or recipient.
    • Reply-To: The email address of the users who fills out the contact form.
    • X-Mailer:: Set the version of the PHP.

Insert and Display Contact Form Data Using PHP and MySQL

Now, to display records on your website, you have to first insert them into your MySQL database using PHP.

First, log in to your PHPMyAdmin to create a database where you can store your contact form users entered data.

login to PHPMyAdmin

Open PHPMyAdmin and enter your username and password.

phpmyadmin-login Contact Form PHP Source Code with MySQL

Open the ‘SQL’ tab of PHPMyAdmin

After PHPMyAdmin login, click the ‘SQL’ tab as shown in the image below.

phpmyadmin-dasboard-click-sql-tab

Create a database to insert PHP contact form data

Now, enter the queries given below to create a database for your contact form to store records. It is the create query of MySQL that you can read more about in the tutorial post on SQL CREATE DATABASE

phpmyadmin-add-code-click-go-to-insert-db Contact Form PHP Source Code with MySQL

Copy and paste the below code under the ‘SQL’ tab textarea and click the ‘Go’ button as shown in the image above to execute the queries.

Connect to Your Database Using PHP

After creating the database, make a connection with your MySQL database using the PHP source code as given in the image below. Also, change the username and password of your MySQL database to make a proper connection.

Insert Contact Form Data Into the Database

To insert the contact form data into a database, you have to execute the MySQL queries using the PHP code given below. ‘contacts’ is the table name in the database containing three columns that are name, email, and message. It uses the SQL insert statement in the MYSQL database.

Display Inserted Data into a Table Using PHP

After insertion of the contact form data into the database, you can display the inserted records using the PHP and MySQL source code as given below. It uses the SQL select statement in the MySQL database.

Your inserted records will get displayed as shown in the image 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.