Guest User

Untitled

a guest
Nov 7th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. <form id="contact-form" method="post" action="contact.php" name="contact-form" role="form">
  2. <div class="form-group">
  3. <i class="fa fa-user grey-text"></i>
  4. <label for="contact-name">Full Name</label>
  5. <input type="text" class="form-control" id="contact-name" name="contact-name"/>
  6. <div class="help-block with-errors"></div>
  7. </div>
  8. <div class="form-group">
  9. <i class="fa fa-envelope grey-text"></i>
  10. <label for="contact-email">Email</label>
  11. <input type="email" class="form-control" id="contact-email" name="contact-email" required="required"aria-describedby="emailHelp" data-error="Please enter your email address"/>
  12. <div class="help-block with-errors"></div>
  13. <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  14. </div>
  15. <div class="messages"></div>
  16. <div class="text-right">
  17. <button type="submit" class="btn btn-custom" name="submit">Submit</button>
  18. </div>
  19. </form>
  20.  
  21. <?php
  22. require 'PHPMailer/src/PHPMailer.php';
  23. require 'PHPMailer/src/Exception.php';
  24. require 'PHPMailer/src/SMTP.php';
  25.  
  26. $mail = new PHPMailerPHPMailerPHPMailer(true);
  27.  
  28. try {
  29. //Server settings
  30. $mail->isSMTP(); // set SMTP
  31. $mail->Host = 'mailserver.com'; // Server
  32. $mail->SMTPAuth = true; // Enable SMTP auth
  33. $mail->Username = 'info@domain.com';
  34. $mail->Password = 'password';
  35. $mail->SMTPSecure = 'tls'; // Enable TLSd
  36. $mail->Port = 587; // TCP port
  37.  
  38. //Recipients
  39. $mail->setFrom('info@domain.com', 'Web Contact'); // FROM
  40. $mail->addAddress('admin@domain.com', 'Admin - Domain'); // TO
  41.  
  42. // message that will be displayed when everything is OK :)
  43. $okMessage = 'Thank you for your message. We will get back to you soon!';
  44.  
  45. // If something goes wrong, we will display this message.
  46. $errorMessage = 'There was an error. Please try again later!';
  47.  
  48. //Content
  49. $mail->isHTML(true); // Set to HTML
  50. $mail->Subject = 'Contact Form Message';
  51. $mail->Body = "Full Name: ".$_POST['contact-name']."<br />Email Address: ".$_POST['contact-email']."<br /><br />";
  52.  
  53. $mail->send();
  54. $responseArray = array('type' => 'success', 'message' => $okMessage);
  55. } catch (Exception $e) {
  56. $responseArray = array('type' => 'danger', 'message' => $errorMessage);
  57. }
  58.  
  59. // if requested by AJAX request return JSON response
  60. if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  61. $encoded = json_encode($responseArray);
  62.  
  63. header('Content-Type: application/json');
  64.  
  65. echo $encoded;
  66. }
  67. // else just display the message
  68. else {
  69. echo $responseArray['message'];
  70. }
  71. ?>
  72.  
  73. $(function () {
  74. $('#contact-form').validator();
  75.  
  76. $('#contact-form').on('submit', function (e) {
  77. // if the validator good
  78. if (!e.isDefaultPrevented()) {
  79. var url = "contact.php";
  80. // POST values in the background the the script URL
  81. $.ajax({
  82. type: "POST",
  83. url: url,
  84. data: $(this).serialize(),
  85. success: function (data)
  86. {
  87. // data = JSON object that contact.php returns
  88. // apply success/danger
  89. var messageAlert = 'alert-' + data.type;
  90. var messageText = data.message;
  91.  
  92. // Bootstrap alert box HTML
  93. var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable" role="alert"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
  94.  
  95. // If we have messageAlert and messageText
  96. if (messageAlert && messageText) {
  97. // inject the alert to .messages div in our form
  98. $('#contact-form').find('.messages').html(alertBox);
  99. // empty the form
  100. $('#contact-form')[0].reset();
  101. }
  102. }
  103. });
  104. return false;
  105. }
  106. })
  107. });
Add Comment
Please, Sign In to add comment