Articles

PHP Contact us Form Example

Simple contact us form submit to database in PHP MySQL with jQuery validation. In this tutorial, you will learn how to create a contact form in PHP, and submit contact us form data in MySQL database with jQuery validation.

This tutorial helps you step by step to create and submit contact forms in PHP + MySQL DB with jQuery validation. And you can use this form for your business queries. The good thing is you can use this contact form in your web application too.

And as well as, you can use the free source code of PHP contact us form with the database source code.

PHP Contact us Form with Validation and Database

Just follow the few below steps and easily create and submit contact form in PHP MySQL with jquery validation.

  • Step 1 – Create Table in Database
  • Step 2 – Create a Database Connection File
  • Step 3 – Create Contact Form in PHP

Step 1 – Create Table in Database

First of all, go to your PHPMyAdmin and create a table name contacts_list with the following fields: name, email, mobile.

CREATE TABLE `contacts_list` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `phone` varchar(50) NOT NULL,
  `subject` varchar(255) NOT NULL,
  `Message` text NOT NULL,
  `sent_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Step 2 – Create a Database Connection File

In this step, you will create a file name db.php and update the below code into your file.

The below code is used to create a MySQL database connection in PHP. When we insert form data into MySQL database, there we will include this file:

Step 3 – Create Contact Form in PHP

In this step, you need to create contact form and add following code into your contact-form.php file:





Contact Form in PHP




\r\n"; // Send email if(mail($toMail, $subject, $message, $header)) { // Store contactor data in database $sql = $connection->query("INSERT INTO contacts_list(name, email, phone, subject, message, sent_date) VALUES ('{$name}', '{$email}', '{$phone}', '{$subject}', '{$message}', now())"); if(!$sql) { die("MySQL query failed."); } else { $response = array( "status" => "alert-success", "message" => "We have received your query and stored your information. We will contact you shortly." ); } } else { $response = array( "status" => "alert-danger", "message" => "Message coudn't be sent, try again" ); } } ?>

Conclusion

In this tutorial, you have learned how to create contact form with jQuery validation and store data into a MySQL database.

Recommended PHP Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

Images mentioned above related to Jquery,MySQL,PHP are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button