Reputation: 362
I'm struggling trying to adapt this method of submitting a form. I'm trying to use JSON and understand what is happening here... but I feel like I'm missing something really obvious... I'd really like to get this working. Thank you!
Here's the HTML
<div class="span4">
<div id="formResponse"></div>
<form id="contactForm" name="contactForm" method="post">
<div class="controls">
<input class="span4" type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="controls">
<input class="span4" type="email" name="email" id="email" placeholder="E-mail" required />
</div>
<div class="controls">
<textarea rows="3" class="span4" name="message" id="message"placeholder="Leave your message at the beep..."></textarea>
</div>
<div class="controls">
<input type="submit" class="btn btn-primary" name="formSubmit" id="formSubmit" value="Send E-Mail" />
</form>
</div>
</div>
Here's the jQuery:
<script type="text/javascript">
$(document).ready(function(){
$('#contactForm').submit(function(){
$.ajax({
type: "POST",
url: "php/contact.php",
data: $("#contactForm").serialize(),
dataType: "json",
success: function(msg){
$("#formResponse").removeClass('error');
$("#formResponse").removeClass('success');
$("#formResponse").addClass(msg.status);
$("#formResponse").html(msg.message);
},
error: function(){
$("#formResponse").removeClass('success');
$("#formResponse").addClass('error');
$("#formResponse").html("There was an error submitting the form. Please try again.");
}
});
});
});
</script>
Here's the PHP:
function checkEmail($email){
if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)){
return FALSE;
}
list($Username, $Domain) = split("@",$email);
if(@getmxrr($Domain, $MXHost)){
return TRUE;
} else {
if(@fsockopen($Domain, 25, $errno, $errstr, 30)){
return TRUE;
} else {
return FALSE;
}
}
}
$response_array = array();
if(empty($_POST['name'])){
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'Name is blank';
} elseif(!checkEmail($_POST['email'])) {
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'Email is blank or invalid';
} elseif(empty($_POST['message'])) {
$response_array['status'] = 'error';
$response_array['message'] = 'Message is blank';
} else {
//send the email
$body = $_POST['name'] . " sent you a message\n";
$body .= "Details:\n\n" . $_POST['message'];
mail($_POST['email'], "SUBJECT LINE", $body);
//set the response
$response_array['status'] = 'success';
$response_array['message'] = 'Email sent!';
}
echo json_encode($response_array);
Upvotes: 0
Views: 1614
Reputation: 1547
You can also remove the
<input type="submit" class="btn btn-primary" name="formSubmit" id="formSubmit" value="Send E-Mail" />
and replace it with
<input type="button" class="btn btn-primary" name="formSubmit" id="formSubmit" value="Send E-Mail" />
Use this in your jquery:-
$('#formSubmit').click(function(){});
instead of:-
$('#contactForm').submit(function(){});
That would prevent the form's default post behaviour. Secondly, if you're running your code on localhost, ensure that you have mail server installed, for using php mail() function.
Upvotes: 1
Reputation: 191789
You didn't specify what's not working, but I can immediately see that you're not preventing default form submission behavior, and that is a problem:
$('#contactForm').submit(function (e) {
e.preventDefault();
That prevents the form from submitting normally so that your ajax will be used instead. I don't see anything else untoward.
Upvotes: 1