RUSSEL86

Ajax Contact Form Resource

Jan 12th, 2018
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.38 KB | None | 0 0
  1. PHP Code:
  2. --------------
  3. <?php
  4.  
  5.    // Only process POST reqeusts.
  6.    if ($_SERVER["REQUEST_METHOD"] == "POST") {
  7.        // Get the form fields and remove whitespace.
  8.        $name = strip_tags(trim($_POST["name"]));
  9.                 $name = str_replace(array("\r","\n"),array(" "," "),$name);
  10.        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
  11.        $lname = trim($_POST["lname"]);
  12.        $subject = trim($_POST["subject"]);
  13.        $message = trim($_POST["message"]);
  14.  
  15.        // Check that data was sent to the mailer.
  16.        if ( empty($name) OR empty($subject) OR empty($lname) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  17.            // Set a 400 (bad request) response code and exit.
  18.            http_response_code(400);
  19.            echo "Please complete the form and try again.";
  20.            exit;
  21.        }
  22.  
  23.        // Set the recipient email address.
  24.        // FIXME: Update this to your desired email address.
  25.        $recipient = "[email protected]";
  26.  
  27.        // Set the email subject.
  28.        $subject = "New contact from $name";
  29.  
  30.        // Build the email content.
  31.        $email_content = "First Name: $name\n";
  32.        $email_content = "Last Name: $lname\n";
  33.        $email_content .= "Email: $email\n\n";
  34.        $email_content .= "Subject: $subject\n\n";
  35.        $email_content .= "Message:\n$message\n";
  36.  
  37.        // Build the email headers.
  38.        $email_headers = "From: $name <$email>";
  39.  
  40.         // Send the email.
  41.         if (mail($recipient, $subject, $email_content, $email_headers)) {
  42.             // Set a 200 (okay) response code.
  43.             http_response_code(200);
  44.             echo "Thank You! Your message has been sent.";
  45.         } else {
  46.             // Set a 500 (internal server error) response code.
  47.             http_response_code(500);
  48.             echo "Oops! Something went wrong and we couldn't send your message.";
  49.         }
  50.  
  51.     } else {
  52.         // Not a POST request, set a 403 (forbidden) response code.
  53.         http_response_code(403);
  54.         echo "There was a problem with your submission, please try again.";
  55.     }
  56.  
  57. ?>
  58.  
  59.  
  60.  
  61.  
  62.  
  63. Ajax jQuery
  64. ----------------------
  65. $(function() {
  66.  
  67.     // Get the form.
  68.     var form = $('#contact-form');
  69.  
  70.     // Get the messages div.
  71.     var formMessages = $('.form-message');
  72.  
  73.     // Set up an event listener for the contact form.
  74.     $(form).submit(function(e) {
  75.         // Stop the browser from submitting the form.
  76.         e.preventDefault();
  77.  
  78.         // Serialize the form data.
  79.         var formData = $(form).serialize();
  80.  
  81.         // Submit the form using AJAX.
  82.         $.ajax({
  83.             type: 'POST',
  84.             url: $(form).attr('action'),
  85.             data: formData
  86.         })
  87.         .done(function(response) {
  88.             // Make sure that the formMessages div has the 'success' class.
  89.             $(formMessages).removeClass('error');
  90.             $(formMessages).addClass('success');
  91.  
  92.             // Set the message text.
  93.             $(formMessages).text(response);
  94.  
  95.             // Clear the form.
  96.             $('#contact-form input,#contact-form textarea').val('');
  97.         })
  98.         .fail(function(data) {
  99.             // Make sure that the formMessages div has the 'error' class.
  100.             $(formMessages).removeClass('success');
  101.             $(formMessages).addClass('error');
  102.  
  103.             // Set the message text.
  104.             if (data.responseText !== '') {
  105.                 $(formMessages).text(data.responseText);
  106.             } else {
  107.                 $(formMessages).text('Oops! An error occured and your message could not be sent.');
  108.             }
  109.         });
  110.     });
  111.  
  112. });
Advertisement
Add Comment
Please, Sign In to add comment