How to Create a Dynamic Contact Form in PHP with MySQL

Last Updated on January 22, 2023 by Roshan Parihar

In this post, learn how to create a dynamic contact form in PHP with MySQL. You can add custom dynamic fields to forms to process their data dynamically.

When you want to capture dynamic data of your audience, you can add dynamically created fields to your contact form. It allows customers to add more custom input fields on button clicks.

How to Create a Dynamic Contact Form in PHP with MySQL

Bonus Tip

Looking to create dynamic contact forms and send emails with data display without coding? Create beautiful-looking contact forms with JotForm.

The customers can fill in the dynamic input fields and you can either collect it via emails or store it in the database. After that, you can make one-to-one communicate with them to solve their queries.

For example, if your customers have some queries regarding multiple products on your website. You can allow customers to enter queries and enter multiple products name using dynamic fields. This shows how much useful it is for businesses.

I am going to show you how to add and remove dynamic input fields to your contact form with a button click. You will also learn how to send the dynamic field data in emails and store it in the MySQL database.

So, let’s get started.

How to Create a Dynamic Contact Form in PHP with MySQL

Here is the step-by-step process to easily create a dynamic contact form in PHP and MySQL:-

Step 1: Create Dynamic Contact Form Design

Firstly, you have to create a beautifully designed contact form with form fields to collect user information. The form fields should collect all the information required to make efficient communication with your audience.

The following fields are an important part of the contact form to collect users information useful for one-to-one communication:-

  • Name
  • Email
  • Message
  • Dynamic Field with ‘Add more’ input button

Below is the simple code of the contact form containing a dynamic field with Name, Email, and Message text area.

To make it look beautiful on your website, let’s add some CSS for the styling of the contact form with the code below.

Styling Dynamic Contact Form

Now, you have added a simple contact form and made it beautiful by adding some CSS code to it. When you see the above code on your browser, the appearance of your contact form will look like the image below.

dynamic contact form design

The above image shows that the form contains fields Name, Email, and Message. It also contains a dynamic field with the add button to add more input fields to your contact form dynamically. You can also delete the dynamically added input field using the remove button.

Step 2: jQuery Script to Add Dynamic Fields to Contact Form

To make the above ‘Add More’ and ‘Remove’ button work to add/remove dynamic input fields on your contact form, you have to add some jQuery script given below.

Now, your form design is completed with a feature to add/remove dynamic input fields on your contact form.

The next step is to add some PHP code to collect your customer’s data in the dynamic contact form and send it in an email to the website owner. So, let’s move further to do this.

Step 3: Send Dynamic Contact Form Data in Email Using PHP

To send dynamic contact form data in email using PHP, see the PHP code given below. It first collects the form data for the Name, Email, and Message using $_POST global variable. After that, validate the email address to find out whether the email address is correct or not.

Let’s understand each variable to send emails using PHP code:-

  • $to: It is the recipient’s email address which is the email address of the website owner.
  • $subject: The subject of the email.
  • $message: The main content of the email to send to the recipient. You can add the captured data of the contact form which are Name, Email, Message, and the Dynamic Field of the contact form.
  • $key: It is the index position of the dynamic values.
  • $value: It is the main value of the dynamic field.
  • $headers:
    • ‘From’ is the email address of the website owner that should be domain specific.
    • ‘CC’ is the email address of the other person who wants to receive the email.
    • “Reply-To’ is the email address of your customer who fills out the contact form.
  • mail(): It is the in-built function in PHP to send emails. It requires 4 variables to pass as an argument of the function to make it work in PHP.

Now, you have sent an email to the website owner.

But, how you can find your customer’s data in the future when you want to make one-to-one communication with them? One best solutions is the store your contact form data in MySQL data and display it somewhere on your website page. This way, you can easily find your customer data when required.

So, let’s insert contact form data with the dynamic field into the database and display it on a website page using PHP and MySQL.

Step 4: Insert Dynamically Created Data Using PHP and MySQL

You have to first create a MySQL database and table inside it to insert your contact form data into it using PHP. The table should contain columns for all the contact form fields which are Name, Email, Message, and Dynamic Field.

Let’s take an example of creating a database with the name ‘contact_form‘ and table ‘contacts‘ inside it with all columns as form fields. Now, let’s move ahead to create the database with the table.

Create MySQL Database to Store Records

To create the database and a table inside it, you have to first log in to your PHPMyadmin. Open PHPMyAdmin and enter your username and password to log in.

PHPMyAdmin login to dashboard How to Create a Dynamic Contact Form in PHP with MySQL

After that, open the SQL tab as indicated in the image below. The SQL tab contains the feature to run the SQL code to create the database and table in it.

PHPMyAdmin dashboard go to SQL tab

You will get a large input text area in the ‘SQL’ tab where you have to copy and paste the below code.

After that, click ‘Go’ to execute your SQL code to create your database with a table to store data in columns.

PHPMyAdmin add code to create database

Now, your database is ready to store records. So, let’s insert some data into it.

But, before you insert data into your database, you have to make a connection with your data. Without making a connection with your database using PHP, you won’t be able to insert dynamic contact form data into it.

So, let’s first make a connection with your database using PHP.

Make MySQL Database Connection

To make connecting with your database, you have to use the below code. It contains the PHP mysqli_connect() that requires 4 arguments that are server name, the username of your database, the password of your database, and your database name.

Now, you have successfully made a connection with your database, you can now capture contact form data and insert it into your database. So, let’s do this.

Insert Data Into MySQL Database with Dynamic Fields

To insert dynamic contact form data into the database, you have to first get the customers entered form data using $_POST global variable.

But, how you can collect dynamic field data of your dynamic contact form? Well! it’s simple when you use an associative array to store it into an array variable.

You can create an array of multiple dynamic input fields with all of the same name. When you check the dynamic contact form design given above, you will get the same name for all dynamically added input fields.

To create an array of multiple dynamic input fields, you have to use the for loop of PHP as given in the below PHP code.

As you cannot be able to insert an array directly into the MySQL database. So, after creating an array of dynamic field data, you have to convert it into a string using serialize() function of PHP. . That’s why converting the array into a string makes it easier to insert it into the database.

After that, you can create an insert SQL in PHP as given in the code below. To execute the query and insert the data into the database, you have to use the mysqli_query() and pass the connection variable and query variable as their first and second argument.

Now, you have successfully inserted the dynamic contact form data into your database. Let’s display the data somewhere on your website page.

Step 5: Display Records in a Table Using PHP and MySQL

To display the dynamic contact form data on your website page, you have to first select the table of the database. As the table name is ‘contacts’, you have to create the ‘select’ SQL query and store it in a variable. After that, execute the query using the mysqli_query() function of PHP.

If you want to display the data only when the database contains some records available in it, you have to use the mysqli_num_rows() as given below.

For displaying the dynamic fields, you have to first convert the dynamic string into an array using the unserialize() function of PHP. After that, use the foreach() function of PHP to display each dynamic field data one by one.

The output of the above code is as showing in the table below in the image.

Display data in a table format

The table contains the columns with names as the input fields of the dynamic contact form. It also contains the customers entered example data. The dynamically added field data are shown in comma(,) separation for each entry.

That’s all.

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.