Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. $("#contactform").submit( function (e) {
  2. e.returnValue=false;
  3. e.preventDefault();
  4. alert('form submitted');
  5. if (checkValidation()) {
  6. //if valid, send ajax
  7. var name=$('#form-name').val();
  8. var email=$('#form-email').val();
  9. var contact_number=$('#form-contact-number').val();
  10. var message=$('#form-message').val();
  11. alert(name+email+contact_number+message);
  12. $.ajax({
  13. type: 'POST',
  14. url: 'mail.php',
  15. //data: JSON.stringify(parameters),
  16. contentType: "json",
  17. // data: $(this).serialize(),
  18.  
  19. data:{'name':name,'email':email,'contact_number':contact_number,'message':message},
  20. dataType: "text",
  21. cache: false,
  22. success: function(data) {
  23. // do something with ajax data
  24.  
  25. $('.form-response').css({color:'black',backgroundColor:'white',textAlign:'center'}).text('Thank you, we will contact you shortly.').show();
  26.  
  27. $('input').val('').trigger('blur');
  28. $('textarea').val('').trigger('blur');
  29. setTimeout(function(){
  30. $('.form-response').hide();
  31. scroll_to_top();
  32.  
  33. },3000);
  34.  
  35. },
  36. error:function (xhr, ajaxOptions, thrownError){
  37. console.log('error...', xhr);
  38. //error logging
  39. },
  40. complete: function(){
  41. //afer ajax call is completed
  42. }
  43. });
  44.  
  45. } else {
  46. alert('Please re-enter your input and try again.');
  47. $('input').val('');
  48. $('textarea').val('');
  49. $("input").trigger("blur");
  50. $("textarea").trigger("blur");
  51. $('#form-name').focus();
  52.  
  53. }
  54.  
  55.  
  56. });
  57.  
  58. <form class="form-style validate-form clearfix" id="contactform" action="mail.php" method="POST" role="form">
  59. <div class="col-md-6"><div class="form-group"><input type="text" class="text-field form-control validate-field required" data-validation-type="string" id="form-name" placeholder="Full Name" name="name"></div>
  60.  
  61. <div class="form-group"><input type="email" class="text-field form-control validate-field required" data-validation-type="email" id="form-email" placeholder="Email Address" name="email"></div>
  62. <div class="form-group"><input type="tel" class="text-field form-control validate-field phone" data-validation-type="phone" id="form-contact-number" placeholder="Contact Number" name="contact_number">
  63. <input type="text" id="address-input" name="address" style="display: none!important;"></div></div><div class="col-md-6">
  64. <div class="form-group"><textarea placeholder="Message..." id="form-message" class="form-control validate-field required" name="message"></textarea></div>
  65. <div class="form-group"><button type="submit" id="submitBtn" class="btn btn-sm btn-outline-inverse">Submit</button></div></div>
  66. </form>
  67.  
  68. <?php
  69. if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' )
  70. {
  71. # is ajax
  72.  
  73. if (empty($_POST["address"])) {
  74. $from = $_POST["name"]; // sender
  75. $subject = 'From: ' . $from;
  76. $email = $_POST["email"];
  77. $tel = $_POST["contact_number"];
  78. $message = $_POST["message"];
  79. // message lines should not exceed 70 characters (PHP rule), so wrap it
  80. $message = wordwrap($message, 70);
  81. $textToSend = 'From: ' . $from . "n";
  82. $textToSend .= 'Email: ' . $email . "n";
  83. $textToSend .= "Phone: " . $tel . "n";
  84. $textToSend .= 'Message: ' . $message . "n";
  85. // send mail
  86. mail("contact@domain.net", $subject, $textToSend, "From: $fromn");
  87. echo "Thank you, we will contact you shortly.";
  88.  
  89.  
  90. echo '
  91. <script>
  92. $("input").val("");
  93. $("textarea").val("");
  94. setTimeout(function(){
  95.  
  96. scroll_to_top();
  97.  
  98. },3000);
  99.  
  100. </script>
  101.  
  102.  
  103.  
  104. ';
  105.  
  106. } else {
  107. echo 'Thank you, we will contact you shortly.';
  108.  
  109. echo '
  110. <script>
  111. $("input").val("");
  112. $("textarea").val("");
  113. setTimeout(function(){
  114.  
  115. scroll_to_top();
  116.  
  117. },3000);
  118.  
  119. </script>
  120.  
  121.  
  122.  
  123. ';
  124. }
  125. }else{
  126. header( 'Location: http://www.myhomepage.net' ) ;
  127. die();
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement