I want to submit my form and that a AJAX Call should send that data to my php file. In my php file is a e-mail validation with filter_var
and if it the validation returns false the respond should get back to Javascript. Besides the validation part it works great. But everytime I want to submit and check if it is a valid email it returns false with "Please enter a valid email". Equal if it is a valid or false email address.
UPDATED - 09.08.2014/04:12
Now it works basically BUT if it has no errormessage I get an Issue Cannot read proberty "isError" of null
which is logical. But don't know how to avoid that issue to pass it correctly back to javascript.
HTML
<form action="submit.php" id="SubmitForm" method="post">
<input id="mail" name="mail" type="text" />
</form>
<button id="submitAll">
<span>Submit</span>
</button>
Javascript
function submit() {
var $form = $('form#SubmitForm');
$.post(
$form.attr('action'),
$form.serialize(),
function (data) {
if (data.isError) {
$errMsg.empty().text('Please enter a valid email');
} else {
document.location.href = "submit.php"
}
},'json'); }
PHP
$profileEmail = $_POST['mail'];
$errorMessages = array();
if(!filter_var($profileEmail, FILTER_VALIDATE_EMAIL)){
$errorMessages[] = "* Please enter a valid e-mail";
} else {
$errorMessages[] = "* Valid e-mail";
}
if (!empty($errorMessages)) {
die(json_encode(array('isError' => true, 'errorMessages' => $errorMessages)));
}