Guest User

Untitled

a guest
Apr 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. <form id="contact-form" method="post" action="contact.php">
  2.  
  3. <div class="messages"></div>
  4.  
  5. <div class="controls">
  6.  
  7. <div class="row">
  8. <div class="col-md-6">
  9. <div class="form-group">
  10. <label for="form_name">Firstname *</label>
  11. <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
  12. <div class="help-block with-errors"></div>
  13. </div>
  14. </div>
  15. <div class="col-md-6">
  16. <div class="form-group">
  17. <label for="form_lastname">Lastname *</label>
  18. <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
  19. <div class="help-block with-errors"></div>
  20. </div>
  21. </div>
  22. </div>
  23. <div class="row">
  24. <div class="col-md-6">
  25. <div class="form-group">
  26. <label for="form_email">Email *</label>
  27. <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
  28. <div class="help-block with-errors"></div>
  29. </div>
  30. </div>
  31. <div class="col-md-6">
  32. <div class="form-group">
  33. <label for="form_phone">Phone</label>
  34. <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
  35. <div class="help-block with-errors"></div>
  36. </div>
  37. </div>
  38. </div>
  39. <div class="row">
  40. <div class="col-md-12">
  41. <div class="form-group">
  42. <label for="form_message">Message *</label>
  43. <textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
  44. <div class="help-block with-errors"></div>
  45. </div>
  46.  
  47. <div class="form-group">
  48. <div class="g-recaptcha" data-sitekey="6LcngVUUAAAAAPRVOHTSu3iGGpFoPF4Cu5Np8E3r" data-callback="verifyRecaptchaCallback" data-expired-callback="expiredRecaptchaCallback"></div>
  49. <input class="form-control d-none" data-recaptcha="true" required data-error="Please complete the Captcha">
  50. <div class="help-block with-errors"></div>
  51. </div>
  52. </div>
  53.  
  54. <div class="col-md-12">
  55. <input type="submit" class="btn btn-success btn-send" value="Send message">
  56. </div>
  57. </div>
  58. <div class="row">
  59. <div class="col-md-12">
  60. <p class="text-muted"><strong>*</strong> These fields are required.</p>
  61. </div>
  62. </div>
  63. </div>
  64.  
  65. </form>
  66. </main>
  67. <!-- Bootstrap JS -->
  68. <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  69. <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
  70. <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
  71.  
  72.  
  73. <!-- Contact Form JS -->
  74. <script src="https://www.google.com/recaptcha/api.js"></script>
  75. <script src="validator.js"></script>
  76. <script src="contact.js"></script>
  77.  
  78. $(function () {
  79.  
  80. window.verifyRecaptchaCallback = function (response) {
  81. $('input[data-recaptcha]').val(response).trigger('change');
  82. }
  83.  
  84. window.expiredRecaptchaCallback = function () {
  85. $('input[data-recaptcha]').val("").trigger('change');
  86. }
  87.  
  88. $('#contact-form').validator();
  89.  
  90. $('#contact-form').on('submit', function (e) {
  91. if (!e.isDefaultPrevented()) {
  92. var url = "contact.php";
  93.  
  94. $.ajax({
  95. type: "POST",
  96. url: url,
  97. data: $(this).serialize(),
  98. success: function (data) {
  99. var messageAlert = 'alert-' + data.type;
  100. var messageText = data.message;
  101.  
  102. var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
  103. if (messageAlert && messageText) {
  104. $('#contact-form').find('.messages').html(alertBox);
  105. $('#contact-form')[0].reset();
  106. grecaptcha.reset();
  107. }
  108. }
  109. });
  110. return false;
  111. }
  112. })
  113. });
  114.  
  115. <?php
  116. // require ReCaptcha class
  117. require('recaptcha-master/src/autoload.php');
  118.  
  119. // configure
  120. // an email address that will be in the From field of the email.
  121. $from = 'infoh@designsingh.ca';
  122.  
  123. // an email address that will receive the email with the output of the form
  124. $sendTo = 'info@designsingh.ca';
  125.  
  126. // subject of the email
  127. $subject = 'New Message from Nirvaani Singh Designs Contact Form';
  128.  
  129. // form field names and their translations.
  130. // array variable name => Text to appear in the email
  131. $fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
  132.  
  133. // message that will be displayed when everything is OK :)
  134. $okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
  135.  
  136. // If something goes wrong, we will display this message.
  137. $errorMessage = 'There was an error while submitting the form. Please try again later';
  138.  
  139. // ReCaptch Secret
  140. $recaptchaSecret = 'secret code';
  141.  
  142. // let's do the sending
  143.  
  144. // if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
  145. error_reporting(E_ALL & ~E_NOTICE);
  146.  
  147. try {
  148. if (!empty($_POST)) {
  149.  
  150. // validate the ReCaptcha, if something is wrong, we throw an Exception,
  151. // i.e. code stops executing and goes to catch() block
  152.  
  153. if (!isset($_POST['g-recaptcha-response'])) {
  154. throw new Exception('ReCaptcha is not set.');
  155. }
  156.  
  157. // do not forget to enter your secret key from https://www.google.com/recaptcha/admin
  158.  
  159. $recaptcha = new ReCaptchaReCaptcha($recaptchaSecret, new ReCaptchaRequestMethodCurlPost());
  160.  
  161. // we validate the ReCaptcha field together with the user's IP address
  162.  
  163. $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
  164.  
  165. if (!$response->isSuccess()) {
  166. throw new Exception('ReCaptcha was not validated.');
  167. }
  168.  
  169. // everything went well, we can compose the message, as usually
  170.  
  171. $emailText = "You have a new message from your contact formn=============================n";
  172.  
  173. foreach ($_POST as $key => $value) {
  174. // If the field exists in the $fields array, include it in the email
  175. if (isset($fields[$key])) {
  176. $emailText .= "$fields[$key]: $valuen";
  177. }
  178. }
  179.  
  180. // All the neccessary headers for the email.
  181. $headers = array('Content-Type: text/plain; charset="UTF-8";',
  182. 'From: ' . $from,
  183. 'Reply-To: ' . $from,
  184. 'Return-Path: ' . $from,
  185. );
  186.  
  187. // Send email
  188. mail($sendTo, $subject, $emailText, implode("n", $headers));
  189.  
  190. $responseArray = array('type' => 'success', 'message' => $okMessage);
  191. }
  192. } catch (Exception $e) {
  193. $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
  194. }
  195.  
  196. if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  197. $encoded = json_encode($responseArray);
  198.  
  199. header('Content-Type: application/json');
  200.  
  201. echo $encoded;
  202. } else {
  203. echo $responseArray['message'];
  204. }
Add Comment
Please, Sign In to add comment