How to develop Contact Us form using PHP and jQuery

In this tutorial we are going to create contact us form using PHP and jQuery. Contact Us form are always needed on website to contact website owner.

How to develop Contact Us form using PHP and jQuery

Create HTML contact us form

<!DOCTYPE html>
  <title>How to develop Contact Us form using PHP and jQuery</title>
  <script src=""></script>
  <script src=""></script>
  <link rel="stylesheet" href="" />
<body class="container">
	<div class="row">
		<div class="account-col" style="width: 450px;text-align: center;margin: 0px auto;">
			<h3>Contact Us</h3>
			<?php if($msg!=1) { 
				echo $msg;
			} ?>
			<form class="m-t" role="form" method="post">
				<div class="form-group">
					<span style="color: #b70a0a;"></span>
				 <div class="form-group">
					<input type="text" class="form-control" name="name" placeholder="Name" required="">
				<div class="form-group">
					<input type="text" class="form-control" name="email" placeholder="Email">
				<div class="form-group">
					<textarea class="form-control" name="message" placeholder="Message"></textarea>
				<input type="submit" class="btn btn-primary btn-block" name="submit" value="submit">

On submit form you can add action or add below code to top of the page. I prefer to add it on top of the HTML file.

		if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])){
			if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
				$msg="Please enter valid email address.";
				$subject = 'Contact Request from '.$_POST['name'];
				$body = '<h2>Contact Request</h2> 
				// Set content-type header for sending HTML email 
				$headers = "MIME-Version: 1.0" . "\r\n"; 
				$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
				// Additional headers 
				$headers .= 'From: '.$_POST['name'].'<'.$_POST['email'].'>'. "\r\n"; 
				// Send email 
				$send = mail($toEmail, $subject, $body, $headers); 
					$msg = 'Your contact request has been submitted successfully.'; 
					$msg = 'Your contact request submission failed, please try again.'; 
			$msg="Please fill all the details.";

You can change $toEmail=””; as per your requirements.

I hope you like this tutorial

Leave a Reply