Created
April 23, 2017 07:03
-
-
Save atcraigwatson/f2924f9b8e9f59803ef196c9a78cd079 to your computer and use it in GitHub Desktop.
Bootstrap Ajax Contact 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 id="contact-form" method="post" action="mailer.php" role="form"> | |
<div class="messages"></div> | |
<div class="controls"> | |
<div class="row"> | |
<div class="col-md-6"> | |
<div class="form-group"> | |
<label for="form_name">Firstname *</label> | |
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required."> | |
<div class="help-block with-errors"></div> | |
</div> | |
</div> | |
<div class="col-md-6"> | |
<div class="form-group"> | |
<label for="form_lastname">Lastname *</label> | |
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required."> | |
<div class="help-block with-errors"></div> | |
</div> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-6"> | |
<div class="form-group"> | |
<label for="form_email">Email *</label> | |
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required."> | |
<div class="help-block with-errors"></div> | |
</div> | |
</div> | |
<div class="col-md-6"> | |
<div class="form-group"> | |
<label for="form_phone">Phone</label> | |
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone"> | |
<div class="help-block with-errors"></div> | |
</div> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-12"> | |
<div class="form-group"> | |
<label for="form_message">Message *</label> | |
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please,leave us a message."></textarea> | |
<div class="help-block with-errors"></div> | |
</div> | |
</div> | |
<div class="col-md-12"> | |
<input type="submit" class="btn btn-success btn-send" value="Send message"> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-12"> | |
<p class="text-muted"><strong>*</strong> These fields are required.</p> | |
</div> | |
</div> | |
</div> | |
</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
$(document).ready( function() { | |
$('#contact-form').validator(); | |
$('#contact-form').on('submit', function (e) { | |
e.preventDefault(); | |
$.ajax({ | |
type: "POST", | |
url: "mailer.php", | |
data: $(this).serialize() | |
}).done( function (data) { | |
var messageAlert = 'alert-' + data.type; | |
var messageText = data.message; | |
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>'; | |
if (messageAlert && messageText) { | |
$('#contact-form').find('.messages').html(alertBox); | |
$('#contact-form')[0].reset(); | |
} | |
}) | |
}) | |
}); | |
}); |
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 | |
// configure | |
$from = '[email protected]'; | |
$sendTo = '[email protected]'; | |
$subject = 'New message from contact form'; | |
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email | |
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!'; | |
$errorMessage = 'There was an error while submitting the form. Please try again later'; | |
// let's do the sending | |
try | |
{ | |
$emailText = "You have new message from contact form\n=============================\n"; | |
foreach ($_POST as $key => $value) { | |
if (isset($fields[$key])) { | |
$emailText .= "$fields[$key]: $value\n"; | |
} | |
} | |
$headers = array('Content-Type: text/plain; charset="UTF-8";', | |
'From: ' . $from, | |
'Reply-To: ' . $from, | |
'Return-Path: ' . $from, | |
); | |
mail($sendTo, $subject, $emailText, implode("\n", $headers)); | |
$responseArray = array('type' => 'success', 'message' => $okMessage); | |
} | |
catch (\Exception $e) | |
{ | |
$responseArray = array('type' => 'danger', 'message' => $errorMessage); | |
} | |
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 { | |
echo $responseArray['message']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent contact form, works very well. Thank you!
I would like to know how to send an attachment file to the email.
Would it be too complicated to implement on the form?