Last active
December 24, 2015 13:09
-
-
Save tomblanchard/6802677 to your computer and use it in GitHub Desktop.
PHP AJAX 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
<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> |
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() { | |
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; | |
}); | |
}); |
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') { | |
$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