Skip to content

Instantly share code, notes, and snippets.

@tomblanchard
Last active December 24, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomblanchard/6802677 to your computer and use it in GitHub Desktop.
Save tomblanchard/6802677 to your computer and use it in GitHub Desktop.
PHP AJAX Form
<form class="contact-form" method="post" action="">
<ul class="form-fields">
<li>
<label>Your Name</label>
<input class="text-input" type="text" name="name" required value="TEST NAME">
</li>
<li>
<label>Your Email</label>
<input class="text-input" type="email" name="email" required value="email@email.com">
</li>
<li>
<label>Subject</label>
<input class="text-input" type="text" name="subject" required value="TEST SUBJECT">
</li>
<li>
<label>Message</label>
<textarea rows="7" name="message" required>TESTING</textarea>
</li>
<li>
<input class="submit-input" type="submit" value="Send" name="contact-submit">
</li>
</ul>
</form>
$(function() {
var form = $('.contact-form');
form.submit(function () {
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: function() {
$.fancybox({'content': 'Email Sent!'});
}
});
return false;
});
});
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = trim(stripslashes($_POST['name']));
$email = trim(stripslashes($_POST['email']));
$subject = trim(stripslashes($_POST['subject']));
$message = trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email@email.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo '<p>Email Sent!</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment