Create Form
<div class="col-sm-6 col-md-6 col-lg-6 contact_left"> <form class="contact_main-form" action="sendMail.php" method="post" id="my_form"> <div class="form-group"> <label class="text_name_c_form">Name:</label> <input type="text" class="form-control input_form_c" id="Email" name="name" required> </div> <div class="form-group"> <label class="text_name_c_form">Email:</label> <input type="email" class="form-control input_form_c" id="Email" name="email" required> </div> <div class="form-group"> <label class="text_name_c_form">Subject:</label> <input type="text" class="form-control input_form_c" id="Email" name="subject" required> </div> <div class="form-group"> <label class="text_name_c_form msg_1">Message:</label> <textarea class="form-control input_form_c text-area" rows="3" name="msg" minlength=50 required></textarea> </div> <button type="submit" name="submit" class="btn btn-default">Send</button> <div class="g-recaptcha" data-sitekey="your key"></div> </form> <div id="server-results"><!-- For server results --></div> </div>
sendMail.php
<?php $email;$message;$captcha; $subject; $ToEmail = 'your@testmail.com'; $EmailSubject = 'Site contact form'; $mailheader = "From: ".$_POST["email"]."\r\n"; $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $name = "Name: ".$_POST["name"].""; $email .= "Email: ".$_POST["email"].""; $sub .= "Subject: ".nl2br($_POST["subject"]).""; $msg .= "Message: ".nl2br($_POST["msg"]).""; $content = '<div style="width:90%;margin:0 auto;text-align:left;padding-left:4px"> <strong>First Name:</strong> '.$name.'<br> <strong>Subject:</strong> '.$sub.'<br> <strong>Email:</strong> '.$email.'<br> <strong>Message:</strong> '.$msg.'<br> .</div>'; if ($_POST["email"]<>'') { if(isset($_POST['email'])){ $email=$_POST['email']; } if(isset($_POST['subject'])){ $subject=$_POST['subject']; } if(isset($_POST['g-recaptcha-response'])){ $captcha=$_POST['g-recaptcha-response']; } if(!$captcha){ echo '<h2>Please check the the captcha form.</h2>'; } $secretKey = "Your Google Secret key"; $ip = $_SERVER['REMOTE_ADDR']; $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); $responseKeys = json_decode($response,true); ?> <?php if(intval($responseKeys["success"]) !== 1) { ?> <?php } else { ?> <?php mail($ToEmail, $EmailSubject, $content , $mailheader) or die ("Failure"); ?> <div class="alert alert-success"> <strong>Thank you for your message! We will answer you within maximum 48h check</strong> </div> <?php } } ?>
Ajax
jQuery("#my_form").submit(function(event){ event.preventDefault(); //prevent default action var post_url = $(this).attr("action"); //get form action url var request_method = $(this).attr("method"); //get form GET/POST method var form_data = new FormData(this); //Creates new FormData object jQuery.ajax({ url : post_url, type: request_method, data : form_data, contentType: false, cache: false, processData:false }).done(function(response){ // $("#server-results").html(response); }); });