Wednesday, 8 October 2014

jQuery Contact Form With Validation & Without Page Refresh

<?php  

//validate 
if ($_POST) { 
//send confirmation email (or insert into database, etc...) 
    echo 'Sent'; die;  
    mail($to,$subject,$comments,$headers); 
} 
// 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Simple jQuery Contact Form Without Page Refresh</title> 

<!--Get jQuery & validate plug-in--> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> 
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script> 

<script> 
$(document).ready(function() {
 $("#form1").validate({
  submitHandler: function() {
   //submit the form
   $.post("<?php echo $_SERVER[PHP_SELF]; ?>", //post
    $("#form1").serialize(), 
    function(data){
      //if message is sent
      if (data == 'Sent') {
        $("#message").fadeIn(); //show confirmation message
        $("#form1")[0].reset(); //reset fields
    }
    //
   });
   return false; //don't let the page refresh on submit.
  }
 }); //validate the form
});
</script> 

<!--style the error message--> 
<style type="text/css"> 
.error { 
    display: block; 
    color: red; 
    font-style: italic; 
} 
#message { 
    display:none; 
    font-size:15px; 
    font-weight:bold; 
    color:#333333; 
} 
</style> 

</head> 
<body> 

<div id="message">Your message has been sent.<br /><br /></div> 

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 

     Name<br /> 
     <input name="name_r" type="text" class="required" id="name_r" /> 
     <br /> 
     <br /> 
     Email<br /> 
     <input name="email_r" type="text" class="required email" size="30" /> 
     <br /> 
     <br /> 
     Comments<br /> 
     <textarea name="comments_r" cols="25" rows="5" class="required"></textarea> 
     <br /> 
     <br /> 
     <input name="Submit" id="submit" type="submit" class="submit_go" value="Submit" /> 
      
</form>

</body> 
</html> 

0 comments:

Post a Comment