Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form id="contact-form" method="post" action="contact.php" name="contact-form" role="form">
- <div class="form-group">
- <i class="fa fa-user grey-text"></i>
- <label for="contact-name">Full Name</label>
- <input type="text" class="form-control" id="contact-name" name="contact-name"/>
- <div class="help-block with-errors"></div>
- </div>
- <div class="form-group">
- <i class="fa fa-envelope grey-text"></i>
- <label for="contact-email">Email</label>
- <input type="email" class="form-control" id="contact-email" name="contact-email" required="required"aria-describedby="emailHelp" data-error="Please enter your email address"/>
- <div class="help-block with-errors"></div>
- <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
- </div>
- <div class="messages"></div>
- <div class="text-right">
- <button type="submit" class="btn btn-custom" name="submit">Submit</button>
- </div>
- </form>
- <?php
- require 'PHPMailer/src/PHPMailer.php';
- require 'PHPMailer/src/Exception.php';
- require 'PHPMailer/src/SMTP.php';
- $mail = new PHPMailerPHPMailerPHPMailer(true);
- try {
- //Server settings
- $mail->isSMTP(); // set SMTP
- $mail->Host = 'mailserver.com'; // Server
- $mail->SMTPAuth = true; // Enable SMTP auth
- $mail->Username = 'info@domain.com';
- $mail->Password = 'password';
- $mail->SMTPSecure = 'tls'; // Enable TLSd
- $mail->Port = 587; // TCP port
- //Recipients
- $mail->setFrom('info@domain.com', 'Web Contact'); // FROM
- $mail->addAddress('admin@domain.com', 'Admin - Domain'); // TO
- // message that will be displayed when everything is OK :)
- $okMessage = 'Thank you for your message. We will get back to you soon!';
- // If something goes wrong, we will display this message.
- $errorMessage = 'There was an error. Please try again later!';
- //Content
- $mail->isHTML(true); // Set to HTML
- $mail->Subject = 'Contact Form Message';
- $mail->Body = "Full Name: ".$_POST['contact-name']."<br />Email Address: ".$_POST['contact-email']."<br /><br />";
- $mail->send();
- $responseArray = array('type' => 'success', 'message' => $okMessage);
- } catch (Exception $e) {
- $responseArray = array('type' => 'danger', 'message' => $errorMessage);
- }
- // if requested by AJAX request return JSON response
- if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
- $encoded = json_encode($responseArray);
- header('Content-Type: application/json');
- echo $encoded;
- }
- // else just display the message
- else {
- echo $responseArray['message'];
- }
- ?>
- $(function () {
- $('#contact-form').validator();
- $('#contact-form').on('submit', function (e) {
- // if the validator good
- if (!e.isDefaultPrevented()) {
- var url = "contact.php";
- // POST values in the background the the script URL
- $.ajax({
- type: "POST",
- url: url,
- data: $(this).serialize(),
- success: function (data)
- {
- // data = JSON object that contact.php returns
- // apply success/danger
- var messageAlert = 'alert-' + data.type;
- var messageText = data.message;
- // Bootstrap alert box HTML
- var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable" role="alert"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
- // If we have messageAlert and messageText
- if (messageAlert && messageText) {
- // inject the alert to .messages div in our form
- $('#contact-form').find('.messages').html(alertBox);
- // empty the form
- $('#contact-form')[0].reset();
- }
- }
- });
- return false;
- }
- })
- });
Add Comment
Please, Sign In to add comment