Spread the love

Hi folks!

Would you like to create a contact form with PHP? how to create a simple contact form with PHP? so in this simple PHP contact form tutorial, we are going to create a contact form with PHP to allow the user to send their feedback, queries and other necessary detail easily.

contact form with php

 

For this purpose, we have been created a simple contact form with PHP along with PHP contact form bootstrap and is well as PHP contact form script with validation.

This PHP contact form tutorial consists of two files one of them is index. Php which is used to process the user data and store into a database.

And the second file contains the mail PHP script used to send data directly through email from the contact form PHP.

create a contact form with PHP, which we have created in this tutorial a” bare-bone” version which you can easily edit by any text editor of your choice and then you can easily integrate into your project.

After this tutorial, you should have a better understanding of how to create a contact form with PHP not only for yourself but also for your clients.

Best of luck!

How to create PHP contact form bootstrap?

Bootstrap is the best framework of twitter which allows a developer to create awesome and responsive website templates very quickly.

Here in this tutorial, we created an HTML form with bootstrap. The design of HTML contact form with bootstrap Is very easy just like a piece of cake what you need actually nothing special, you just need a couple of things.

Like the very first thing, you have to do is to download bootstrap and jquery and then integrate it in your working page.

After that, you have to start the creation of the form by using the form tags <form></form>  as you can see in our email form below. Our whole form code or attributes will go between these two form tags which you can see in the below HTML form

 

The above PHP contact form layout consists of four fields which are name, email, subject, message which have been shown to the user using simple HTML and bootstrap. It also consists of some special method and attributes which need to be explained.

The above form the action attribute is used for the specification of action on a particular page.  And the Post-method.

There is usually two very popular superglobal variables are used to submit form data .which  are GET [] method AND POST[] method. Here in this tutorial, we have been used post method -is basically a superglobal array which gets data from the form fields and securely sends that data to database without a show in the URL box. Another important attribute is enctype-enctype is a special attribute which is necessary for the submission of multimedia data through form otherwise the form data will not be submitted. Here in this tutorial our primary focus on the function of the form as to how to store the submitted form data in the database and how to send the data an email form. We also include jquery validation script which returns value in the Boolean form if it returned true the data will be submitted to PHP script but when it returns false then data cannot be sent to the PHP script for processing and shown the error message to the user for correction the data.

Create a Contact form with PHP: how to actually validate it with jquery?

Whenever you want to create HTML email or contact form on your website you should have to validate the form because it is not only to guarantee the correction of data but also it a better experience for a best user-friendly interface. for this purpose, we use different things one of them is jquery.

 

Jquery is a very popular JavaScript library which you have to download from the jquery official site and then just include in your working file.

 

The validation of form with jquery is not that much difficult as people think about it!

Usually, the validation of the form is performed in the two-way client as well as on server-side validation.

Majority of developers recommend both kinds of validation.

Now for the client-side validation, we normally use jquery. Today a lot of jquery plugins are available to validate a form .here in this PHP contact form tutorial our focus just on the manual use of  jquery to validate the contact form

Jquery is mostly used for front-end validation of a form to prevent the submission of inaccurate data.

Below in this tutorial, we have been explained that how could we do the form validation in jquery.?

 

Before going to write PHP script we have to explain the above jquery code briefly.

In the above code “$” is basically a jquery selector which is used to select jquery document. As you can see In the above jquery code, there are four ids which we declared in HTML “h5”  header tags to display the message in the form.

Initially, we have been hiding them using jquery hide function.

after that, we also have been declared four Boolean variable name error, email_error, sub_error, msg_error using the var keyword.

After that, we take the id of every field to access the value of that specific field to check the user queries for validation using the jquery key up function.

 

 

In this step, we have configured the MySQL database for our HTML contact form to store user information for this purpose you just need to go to PHPMyAdmin dashboard and create a database of your choice and then create a table according to your form requirement.

Here in this tutorial,  we have been creating PHP contact us form with the database as you can see

we have been created a database of the name “mydb” and a table named  user_message with five fields  user_id, user _name,user_email,user_subject, and user_message.

Create a Database connection:

 

And then connect the  database with your working file by using the following PHP script

In the above short PHP script, the $con is the variable used to store the connection and the mysqli_conect function used to establish the connection with the database it takes four parameters

First one is the server name which is” localhost”  in this case and the second one is “ root” which is the user of the database and the third one is the “password” which is null in  this case and the last one is the database name which is “mydb” in this tutorial.

create a contact form with PHP basic script:

Right here our contact form with PHP is not functional, means it doesn’t perform any action on user input data because we do not yet write the PHP  for the contact form to process data on server-side.

The creation of HTML contact form with PHP is very easy you just include the following PHP.

Following are the basic PHP  code for contact form which use to processes the form data.

 

The above PHP code will help you to stored data into MySQL database after submission.

As we mentioned above we have to create a database for the contact form with and a table for user information to store user data. after the database creation uses form field and gets user data from the superglobal array  $_POST[].

Then set the PHP insert query and used the mysqli_query function of PHP to the insert query when the query execute that data is stored in the database.

 

The above PHP code is very simple it just gets all the data from the field which we have been specified in the above HTML email form

As you can see the above four tags  name, email, subject, message through which we get data from the form field these are the information which we want to send  from our e-mail form

The mail PHP script retrieves user data using superglobal array   “$_POST[]” and then set header and body of the message and send through PHP mail function, once the email is received successfully then we show a success message to the user that your data has been sending successfully.

for more tutorial  visits the following links

1.  How PHP send email from an application using PHPMailer

2.  How  Php Create Pdf  Files From Html / Mysql

3. How to create simple pagination in PHP and MSQLI?