Specifically, it runs after the first input field has been validated, when I want it to run after the form has been validated fully and posted.
I have an HTML form which is sent off to be validated and posted via a PHP script.
I'm using AJAX to communicate between the 2 files without redirecting my page.
I'm also trying to use the ajaxForm plugin to do two things once the form has been validated and posted 1)Pop up an alert box for the user, and 2) reset the form values back to default.
Therein lies my problem. the ajaxForm function executes before the php script has had a chance to validate all the variables.
Here's some code, please let me know if I can provide anything else useful:
jQuery & AJAX
Code:
<script type="text/javascript">
// Declares variable for red error message text
var redError;
// jQuery Form Plugin options
var myFormOptions = {
target: window,
success: formAfterSubmit,
resetForm: true
};
function formAfterSubmit() {
// PERFORM ACTIONS AFTER FORM SUBMIT HERE
alert("Thank you for your comments!");
}
$(document).ready(function(){
//This is the ajax code for submitting the Quick Contact Form at the bottom.
$("#quick_contact_form").ajaxForm(myFormOptions).submit(function(){
$('#formResponse').append('<img src="ajax-loader.gif" class="loaderIcon" alt="Loading..." />');
$.ajax({
type: "POST",
url: "postForm.ajax.php",
data: $("#quick_contact_form").serialize(),
dataType: "json",
success: function(msg){
$("#quick_contact_form [name='" + msg.inputName +"']").removeClass('error');
$("#quick_contact_form [name='" + msg.inputName +"']").removeClass('success');
$("#quick_contact_form [name='" + msg.inputName +"']").addClass(msg.status);
$("#quick_contact_form [name='" + msg.inputName +"']").val(msg.message);
$('#quick_contact_form img.loaderIcon').fadeOut(1000);
redError = msg.message;
},
error: function(){
$("#quick_contact_form [name='" + msg.inputName +"']").removeClass('success');
$("#quick_contact_form [name='" + msg.inputName +"']").addClass('error');
$("#quick_contact_form [name='" + msg.inputName +"']").html("There was an error, please try again.");
}
});
//make sure the form doesn't post
return false;
});
});
</script>
PHP Script
PHP Code:
<?php
//function to validate the email address
//returns false if email is invalid
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 with status code and message
$response_array = array();
//validate the post form
//check the name field
if(empty($_POST['full_name']) || $_POST['full_name']=='FULL NAME *'){
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'Please enter your name.';
$response_array['inputName'] = 'full_name';
//check the email field
} elseif(!checkEmail($_POST['email_address'])) {
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'Email is blank or invalid.';
$response_array['inputName'] = 'email_address';
//check the message field
} elseif(empty($_POST['quick_contact_msg']) || $_POST['quick_contact_msg']=='QUESTION *' || $_POST['quick_contact_msg']=='Please enter your message!') {
//set the response
$response_array['status'] = 'error';
$response_array['message'] = 'Please enter your message!';
$response_array['inputName'] = 'quick_contact_msg';
//form validated. send email
} else {
//send the email
$body = $_POST['full_name'] . " sent you the following message via Quick Contact Form:<br/>\n\n";
$body .= "<br/><strong>Email Address:</strong>\n" . $_POST['email_address'];
$body .= "<br/><strong>Phone Number:</strong>\n" . $_POST['phone_number'];
$body .= "<br/><strong>Message:</strong><br/>\n\n" . $_POST['quick_contact_msg'];
$to = "email1@email1.com";
$subject = "Quick Contact";
$from = 'MIME-Version: 1.0' . "\r\n";
$from .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$from .= "From: " . $_POST['full_name'] . "\r\nReply-To: " . $_POST['email_address'] . "\r\nBcc: email2@email2.com";
mail($to, $subject, $body, $from);
//grab first name
list( $fname, $mname, $lname ) = explode( ' ', $_POST['full_name'], 3 );
if ( is_null($lname) ) //Meaning only two names were entered...
{
$lastname = $mname;
}
else
{
$lname = explode( ' ', $lname );
$size = sizeof($lname);
$lastname = $lname[$size-1];
}
//set the response
$response_array['status'] = 'success';
$response_array['message'] = 'Thanks, ' . $fname . '! Email sent.';
}
//send the response back
echo json_encode($response_array);
?>