I've an html form which I validate with Php using Jquery. After successfully inserting data to a database it's showing a success message in an html
page and redirect to different page. Well, it's working fine but if there is an error it's also redirecting to a different page.
What I want:
If there is an error it's should stay on this page. If there is no error then it's should redirect to a different page. How can I do this with jquery?
<center><img id="loading-image" src="images/loading-image.gif" style="display:none; margin-bottom:10px !important;"/></center>
<div id="result"></div>
$('body').on('click', '#request_tutor', function (e) {
e.preventDefault();
var formData = new FormData($(this).parents('form')[0]);
$("#loading-image").show();
$.ajax({
url: 'request_tutor_process.php',
type: 'POST',
xhr: function () {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
success: function (data) {
$("#loading-image").hide(); //hide loading
$("#result").html(data);
setTimeout(function () {
$('input[type=submit]').attr('disabled', false);
window.location.href = "login/index.php";
}, 2000);
},
complete: function () {
$("#loading-image").hide(); //hide loading here
},
data: formData,
cache: false,
contentType: false,
processData: false
});
});
Php page:
$err = array();
// required eata validation checking...
if(isset($name) && isset($tutoring_location) && isset($pcode) && isset($mphone) && isset($email) && isset($tutor_level)){
if(empty($name) && empty($tutoring_location) && empty($pcode) && empty($mphone) && empty($email) && empty($tutor_level)){
$err[] = "Please fill up required field which is indicated by *";
}else{
if(empty($name))
$err[] = "Your name required";
elseif(strlen($name) > 30)
$err[] = "Your name is too long";
if(empty($tutoring_location))
$err[] = "Write your tutoring location";
elseif(strlen($tutoring_location) > 255)
$err[] = "Tutoring location address is too long";
if(empty($pcode))
$err[] = "Postal code required";
elseif(strlen($pcode) > 6)
$err[] = "Invalid postal code";
elseif(!is_numeric($pcode))
$err[] = "Invalid postal code";
if(empty($mphone))
$err[] = "Mobile phone number required";
elseif(strlen($mphone) > 8)
$err[] = "Invalid mobile phone number";
elseif(!is_numeric($mphone))
$err[] = "Invalid mobile phone number";
if(empty($email))
$err[] = "Your email address required.";
elseif(@!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
$err[] = "Invalid email address";
elseif($num_email == 1)
$err[] = "Email address <strong>$email</strong> is already exits, please choose another";
if(empty($tutor_level))
$err[] = "Select your tutor level";
}
if(!empty($err)){
echo "<div class='error'>";
foreach($err as $er){
echo "$er. <br/>";
}
echo "</div>";
}else{
ecoh "success";
}
}