[ 기타 활동 ]/HTML5 & CSS

How to Create a PHP Contact Form With MySQL & HTML5 Validation

유니시티황 2018. 7. 21. 03:58

A contact form on your site allows visitors to communicate with the site owner. By using this form, you can keep your email address relatively safe from unwanted emails. Plus, you can easily connect it to your database and keep a record of users who are trying to contact you. After that, you can easily contact them via their email addresses.

Prerequisites

For the purpose of this tutorial, I assume that you have a PHP application installed on a web server. My setup is:

  • PHP 7.1
  • MySQL
  • Jquery/Ajax

To make sure that that I don’t get side-tracked by server level issues, I decided to host my PHP application on Cloudways managed servers because the Platform offers a powerful Laravel optimized environment. In addition, I don’t have to deal with server management hassles and thus focus on the core idea of this tutorial. You can try out Cloudways for free by signing for an account and then following this simple GIF for setting up the server and the Laravel application.

Create a Contact Form

Create a contact form giving below with simple HTML5 validation and save it with .php extension. Value which will be written between the double quotes in attribute Name like name=”u_name” in input tags work as a variable name. These attributes will contain the data from the form that we will use to save in our database . There are two methods to send your form data to your PHP page: GET and POST. To learn the difference between them, click here. I will be using POST as it hides the user data and there is no limit to send data.

Note: For styling you can use your own CSS and also use Bootstrap Classes for better styling.

php hosting signup

Configure the Database

The next step is ro setup and configure the MySQL database. For this, fire up the Cloudways Database manager and create a table ‘contact_form_info’, with the fields  id name , email , phone,comments.

Next, create config.php that will be used to set up the connection between the PHP app and the database. once the file has been created, open it and paste the following code in it:

You might also like: How to Connect to a Remote MySQL Database

Create the PHP Script

Now let’s create a file get_response.php and paste the following code in it:

Since, I have used the POST method for submitting the form data to the server, I will use two global methods, $_REQUEST and $_POST to retrieve and save the data in the server local variable. The difference between these two is that $_REQUEST can retrieve data from both methods i.e. GET and POST. However, $_POST can only receive data from the POST method.

php hosting signup

Here is what the script looks in action:

 

Mail Method

I also create a file contact_mail.php for mail in which send your contact form data on your mail easily.

Conclusion

In this tutorial, I demonstrated creating a Contact Form using AJAX, jQuery and MySQL. Here is a live demo of the application where you could see the form in action. If you need help, just drop a comment below and I will get back to you.