PHP Feedback Form

Last Updated on February 25, 2024 by Roshan Parihar

In this post, learn about the PHP feedback form and the script for validation and send form data via email. You will also get PHP code to insert customers’ data into the database and display it within a table.

If you want to collect the opinions of customers about your products, a feedback form can be the best solution for you to collect. You can display a feedback form on your website with products.

PHP Feedback Form

Bonus Tip

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

Each time when your customers want to give feedback, they can fill out the feedback form to reach you. After collecting customer feedback data, you can improve your products and the performance of your business.

You can easily create a feedback form and collect its data via emails using PHP. The PHP feedback form sends emails only after getting valid entries from customers. It also inserts form data into the database when every entry is valid.

So, let’s get started.

PHP Feedback Form Appearance

The feedback form should contain all the necessary fields to easily collect the opinions of customers. Longer-size feedback forms are difficult for customers to fill. That’s why, to make the feedback form as easy and simple I have decided to add only the below given form fields:-

  • Name
  • Email Address, and
  • Phone number
  • Feedback type
  • Description of your feedback

The above fields are enough to easily collect customer feedback and useful data. The HTML code for the feedback form is given below.

The appearance of the form in HTML is shown in the image below. The interface does not look to add to your website. You have to consider adding some CSS for the styling of your form and give a better user experience.

Feedback Form in HTML

To add styling to your feedback form, you have to add some CSS code as given below. It adds some styling to your form input fields, textarea, and the submit button of the form. You can change the submit button color according to your website brand color.

Add Style to Feedback Form

After adding some CSS for the styling of your feedback form, your form appearance will change like the image shown below. Now, the interface and design look better and are ready to add to your website. It is also responsive to easily resize itself to any screen size like desktop, laptop, tablet, and mobile.

Feedback Form Format

Now, let’s add some validation to your feedback form to collect valid feedback data from customers. After that, you can collect valid opinion data via email with the PHP code given below.

PHP Feedback Form Add Validation and Send Data Via Emails

To make your field required to fill for customers, you have to add required attribute to your form fields. To know your customers, you have to collect valid emails and phone numbers from customers. So, the main required validation here is for email format and Phone number format.

Validate Phone Number Using Javascript

Set the phone number input field type as input type="tel". To get the phone number in numeric format, you have to add Javascript validation with the code given below. You can make the below code work for the phone number field by adding an attribute oninput="validationall('tel', '0-9')".

Also, if you want to allow customers to enter only 10-digit phone numbers, you have to add an attribute maxlength = "10". It restricts customers to type only 10 digits for the phone number field. So, your input field for the phone number will change as given below.

Validate Email Address Using PHP

The below example code matches the email address regex expression with the customer’s input email address to find the valid email data. If the email address is not valid, it displays error messages to customers.

Please see the email address validation code below using PHP to get an idea.

Send Form Data Via Email

To collect customers’ entered data via email to your email address, you can use SMTP in PHP. For this, you have to first download the PHPMailer library with the button given below. You have to extract the PHPMailer library where your main PHP file is located.

Download PHPMailer

After that, use the below code just after the above PHP code. If every customer’s entered form data is correct and validated, it executes the below code to send the form data via email.

Note: don’t forget to change the $sender value with your Gmail address, $gmailapppassword value with the Gmail app password, and the recipient email address with your email address and name.

Now, next is to insert the customer’s feedback data into the MySQL database and display it on a page. Displaying feedback is useful to show new customers and gain more customers for your business. So, let’s do this in the next section.

Insert Form Data Into Database and Display in a Page

In this section, you will learn to insert your feedback form data into the database using PHP. This is useful to get your customer’s feedback and details again to easily call them when required. After you insert the data, you can display the data anywhere on your website.

Firstly, you have to open PHPMyadmin of your MySQL database.

login to PHPMyAdmin

phpmyadmin-login PHP

After that, open the ‘SQL’ tab there in PHPMyAdmin.

Open the ‘SQL’ tab of PHPMyAdmin

phpmyadmin-dasboard-click-sql-tab

Create a database to insert PHP Feedback form data

Now, copy the below SQL code and add it to the input box in the ‘SQL’ tab as shown in the image below. After that, press the ‘Go’ button to execute the SQL code and create a database with the name ‘create_forms’.

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

The below SQL code also creates a table ‘feedback_form’ with the creation of the database ‘create_forms’. This is the table where you have to store your customer’s data entered in the feedback form.

Now, you are ready with your database and a table with all the required fields to easily insert the form data into the MySQL database.

Connect to Your Database Using PHP

After you create the database, you have to connect with the database using the PHP code given below. The code requires the four arguments for PHP mysqli_connect() which are the server name, the username of the database, the password of the database user, and the database name.

If you are connected to your local computer system, you can use the below values for these variables. However, if you want to connect to your hosting server, you have to change these with the database user and password with all privileges.

The code also displays a success message after a successful connection is made with the database.

Insert Feedback Form Data Into the Database

Now, to insert the feedback form data into the database, you have to use the PHP code given below. The code for the PHP feedback form inserts the data into the database using the SQL insert query.

Now, you have successfully sent the email and inserted the form data into the database. It’s time to display the customers entered data on your website posts and pages. This can make it easier for the website owners to keep a record of the customers.

Display Inserted Data in a Table

To display the data in a table, you have to first select the table ‘feedback_form’ using a select query. It first checks whether it contains customers’ records or not. When it contains some data, it displays all the data in a table using a while loop in PHP.

You will get the customer’s data in a table format on your website as shown in the image below. To make your table look better on your website, you can consider adding some CSS code for styling as given below.

You will get the feedback data as shown in the image below.

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

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.