Contact form is one of the basic and most used feature in almost every site. Purpose of contact form is to get suggestion or feedback.
I already covered contact form using ajax in my earlier posts. In this post I am going to create PHP contact form with jquery validation and email sending code. I am going to create 4 input fields and I will use bootstrap to create form template.
Steps involved in PHP Contact Form :
- Create contact form.
- Add jQuery Validations
- Submit form and apply PHP validation
- Submit data into mysql database
- Send thank you email to user
Also Read:
Contact Form HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<div class="container"> <div class="row"> <div class="col-md-12"><h3>Contact Form</h3></div> <?php if(isset($errorMsg)) { echo '<div class="col-md-12">'; echo "<div class='alert alert-danger'>"; echo $errorMsg; echo "</div>"; echo "</div>"; unset($errorMsg); } if(isset($successMsg)) { echo '<div class="col-md-12">'; echo "<div class='alert alert-success'>"; echo $successMsg; echo "</div>"; echo "</div>"; unset($successMsg); } ?> <div class="col-md-6"> <form id="contactForm" method="post" action="" enctype="multipart/form-data"> <div class="form-group"> <label>Name <span class="req">*</span></label> <input type="text" id="name" name="name" class="form-control" placeholder="Name"> </div> <div class="form-group"> <label>Email <span class="req">*</span></label> <input type="text" id="email" name="email" class="form-control" placeholder="Email"> </div> <div class="form-group"> <label>Subject <span class="req">*</span></label> <input type="text" id="subject" name="subject" class="form-control" placeholder="Subject"> </div> <div class="form-group"> <label>Message <span class="req">*</span></label> <textarea class="form-control" id="message" name="message" placeholder="Type your message"></textarea> </div> <div class="form-group"> <button type="submit" id="submit" class="btn btn-primary" name="submit">Submit</button> </div> </form> </div> </div> </div> |
jQuery Validation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<script> $(document).ready(function(){ checkBlurValidation("name"); checkBlurValidation("email"); checkBlurValidation("subject"); checkBlurValidation("message"); $("#submit").click(function(){ var name = $("#name").val(); var email = $("#email").val(); var subject = $("#subject").val(); var message = $("#message").val(); if(name !='' && email!='' && subject!='' && message!='') { return true; } else { return false; } }); function checkBlurValidation(inputID) { $("#"+inputID).blur(function(){ var getValue = $(this).val(); if(getValue == '') { $(this).css("border", "1px solid red"); } else { $(this).css("border", "1px solid green"); } }); } }); </script> |
Create Database:
1 2 3 |
create database demo; |
Create Table
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CREATE TABLE 'contact_form' ( 'id' int(11) NOT NULL, 'name' varchar(255) NOT NULL, 'email' varchar(255) NOT NULL, 'subject' varchar(255) NOT NULL, 'message' text NOT NULL, 'created' date NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE 'contact_form' ADD PRIMARY KEY ('id'); |
PHP Code after Form Submission:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php $conn = mysqli_connect('localhost','root','root','demo'); if(!$conn) { die(mysqli_error()); } if(isset($_POST['submit'])) { if(($_POST['name'] == '') || ($_POST['email'] == '') || ($_POST['subject'] == '') || ($_POST['message'] == '') ) { $errorMsg = "All fields are required"; } else { $name = trim($_POST['name']); $email = trim($_POST['email']); $subject = trim($_POST['subject']); $message = trim($_POST['message']); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errorMsg = "Email address is invalid"; } else { $date = date('Y-m-d'); $qry = "insert into contact_form (name, email, subject, message, created) values ('".$name."','".$email."' , '".$subject."', '".$message."', '".$date."')"; $rs = mysqli_query($conn, $qry); if(mysqli_affected_rows($conn) == 1) { //start mailing code $to = "somebody@example.com"; $subject = "Conact Form"; $message = " <html> <head> <title>Contact Form</title> </head> <body> <p>Thank you! We will contact you soon</p> "; $message.="<table>"; $message.="<tr><td>Name</td><td>".$name."</td></tr>"; $message.="<tr><td>Email</td><td>".$email."</td></tr>"; $message.="<tr><td>Subject</td><td>".$subject."</td></tr>"; $message.="<tr><td>Message</td><td>".$message."</td></tr>"; $message.="</table>"; $message .= "</body></html>"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From: <info@yoursite.com>' . "\r\n"; mail($to,$subject,$message,$headers); $successMsg = "Thank you for contacting us"; } } } } ?> |
PHP Contact form all together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
<?php $conn = mysqli_connect('localhost','root','root','demo'); if(!$conn) { die(mysqli_error()); } if(isset($_POST['submit'])) { if(($_POST['name'] == '') || ($_POST['email'] == '') || ($_POST['subject'] == '') || ($_POST['message'] == '') ) { $errorMsg = "All fields are required"; } else { $name = trim($_POST['name']); $email = trim($_POST['email']); $subject = trim($_POST['subject']); $message = trim($_POST['message']); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errorMsg = "Email address is invalid"; } else { $date = date('Y-m-d'); $qry = "insert into contact_form (name, email, subject, message, created) values ('".$name."','".$email."' , '".$subject."', '".$message."', '".$date."')"; $rs = mysqli_query($conn, $qry); if(mysqli_affected_rows($conn) == 1) { //start mailing code $to = "somebody@example.com"; $subject = "Conact Form"; $message = " <html> <head> <title>Contact Form</title> </head> <body> <p>Thank you! We will contact you soon</p> "; $message.="<table>"; $message.="<tr><td>Name</td><td>".$name."</td></tr>"; $message.="<tr><td>Email</td><td>".$email."</td></tr>"; $message.="<tr><td>Subject</td><td>".$subject."</td></tr>"; $message.="<tr><td>Message</td><td>".$message."</td></tr>"; $message.="</table>"; $message .= "</body></html>"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From: <info@yoursite.com>' . "\r\n"; mail($to,$subject,$message,$headers); $successMsg = "Thank you for contacting us"; } } } } ?> <!DOCTYPE html> <html> <head> <title>Contact Form</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous"> <style> .req{ color:red; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"><h3>Contact Form</h3></div> <?php if(isset($errorMsg)) { echo '<div class="col-md-12">'; echo "<div class='alert alert-danger'>"; echo $errorMsg; echo "</div>"; echo "</div>"; unset($errorMsg); } if(isset($successMsg)) { echo '<div class="col-md-12">'; echo "<div class='alert alert-success'>"; echo $successMsg; echo "</div>"; echo "</div>"; unset($successMsg); } ?> <div class="col-md-6"> <form id="contactForm" method="post" action="" enctype="multipart/form-data"> <div class="form-group"> <label>Name <span class="req">*</span></label> <input type="text" id="name" name="name" class="form-control" placeholder="Name"> </div> <div class="form-group"> <label>Email <span class="req">*</span></label> <input type="text" id="email" name="email" class="form-control" placeholder="Email"> </div> <div class="form-group"> <label>Subject <span class="req">*</span></label> <input type="text" id="subject" name="subject" class="form-control" placeholder="Subject"> </div> <div class="form-group"> <label>Message <span class="req">*</span></label> <textarea class="form-control" id="message" name="message" placeholder="Type your message"></textarea> </div> <div class="form-group"> <button type="submit" id="submit" class="btn btn-primary" name="submit">Submit</button> </div> </form> </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script> <script> $(document).ready(function(){ checkBlurValidation("name"); checkBlurValidation("email"); checkBlurValidation("subject"); checkBlurValidation("message"); $("#submit").click(function(){ var name = $("#name").val(); var email = $("#email").val(); var subject = $("#subject").val(); var message = $("#message").val(); if(name !='' && email!='' && subject!='' && message!='') { return true; } else { return false; } }); function checkBlurValidation(inputID) { $("#"+inputID).blur(function(){ var getValue = $(this).val(); if(getValue == '') { $(this).css("border", "1px solid red"); } else { $(this).css("border", "1px solid green"); } }); } }); </script> </body> </html> |