Last active
June 6, 2016 02:13
-
-
Save 1forh/868cbdab92003e645c18 to your computer and use it in GitHub Desktop.
PHP and AJAX Form Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="form-messages"></div> | |
<form action="mailer.php" method="POST" id="ajax-contact" class="contact-form"> | |
<p>All fields are required.</p> | |
<label for="name"> | |
Full Name | |
<input type="text" name="name" id="name" required> | |
</label> | |
<label for="email"> | |
Email Address | |
<input type="email" name="email" id="email" required> | |
</label> | |
<label for="message"> | |
Message | |
<textarea name="message" id="message" required></textarea> | |
</label> | |
<input type="submit" name="submit" value="Submit" id="submit" class="button"> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
'use strict'; | |
var form = $('#ajax-contact'); | |
var formMessages = $('#form-messages'); | |
// Set up an event listener for the contact form. | |
$(form).submit(function(event) { | |
// Stop the browser from submitting the form. | |
event.preventDefault(); | |
var formData = $(form).serialize(); | |
$.ajax({ | |
type: 'POST', // specifies the HTTP method that will be used for the request | |
url: $(form).attr('action'), // location of the script that the form data will be sent to | |
data: formData | |
}) | |
.success(function(response) { | |
// Make sure that the formMessages div has the 'success' class. | |
$(formMessages).removeClass('error'); | |
$(formMessages).addClass('success'); | |
// Set the message text. | |
$(formMessages).text(response); | |
// Clear the form. | |
$('#name').val(''); | |
$('#email').val(''); | |
$('#message').val(''); | |
ga('send', 'pageview', '/thank-you.html'); | |
}) | |
.fail(function(data) { | |
// Make sure that the formMessages div has the 'error' class. | |
$(formMessages).removeClass('success'); | |
$(formMessages).addClass('error'); | |
// Set the message text. | |
if (data.responseText !== '') { | |
$(formMessages).text(data.responseText); | |
} else { | |
$(formMessages).text('Oops! An error occured and your message could not be sent.'); | |
} | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
$subject = "Message from contact form on http://projects.zacharyminner.com/php-form"; | |
$sendto = "someemail@gmail.com"; | |
// $carbonCopy = "someotheremail@gmail.com"; | |
//Do not edit below this line// | |
$name = strip_tags($_POST["name"]); | |
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); | |
$message = strip_tags($_POST["message"]); | |
// Check that data was sent to the mailer. | |
if ( empty($name) OR empty($email) OR empty($message) ) { | |
// Set a 400 (bad request) response code and exit. | |
http_response_code(400); | |
echo "Oops! There was a problem with your submission. Please complete the form and try again."; | |
exit; | |
} else if ( !filter_var($email, FILTER_VALIDATE_EMAIL) ) { | |
// Set a 400 (bad request) response code and exit. | |
http_response_code(400); | |
echo "Oops! Please enter a valid email address and submit the form again."; | |
exit; | |
} | |
$headers = "From: " . $email . "\r\n"; | |
$headers .= "Reply-To: ". $email . "\r\n"; | |
// $headers .= "CC: " . $carbonCopy . "\r\n"; | |
$headers .= "MIME-Version: 1.0\r\n"; | |
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; | |
$emailContents = "<html><body>"; | |
$emailContents .= "<p>Full Name: " . $name . "</p>"; | |
$emailContents .= "<p>Email Address: " . $email . "</p>"; | |
$emailContents .= "<p>Message: " . $message . "</p>"; | |
$emailContents .= "</body></html>"; | |
$send = mail($sendto, $subject, $emailContents, $headers); | |
if ($send) { | |
http_response_code(200); | |
echo "Your message has been sent.\r\n"; | |
} else { | |
// Set a 500 (internal server error) response code. | |
http_response_code(500); | |
echo "There was a problem sending the email.\r\n"; | |
} | |
} else { | |
// Not a POST request, set a 403 (forbidden) response code. | |
http_response_code(403); | |
echo "There was a problem with your submission, please try again."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a functioning demo version. http://projects.zacharyminner.com/php-form/