我有一个html表单,我用Php使用Jquery验证。成功将数据插入数据库后,它会在html
页面中显示成功消息并重定向到不同的页面。好吧,它工作正常,但如果有错误,它也会重定向到不同的页面。
我想要的是什么:
如果有错误,应该留在此页面上。如果没有错误,那么它应该重定向到另一个页面。我怎么能用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页面:
$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";
}
}
答案 0 :(得分:0)
这可能是罪魁祸首
setTimeout(function () {
$('input[type=submit]').attr('disabled', false);
window.location.href = "login/index.php";
}, 2000);
代码成功后,您将设置一个全局超时,每两秒重定向一次页面。出于某种原因,由于您没有显示PHP代码,因此它总是成功的。基本上,只要响应返回有效(HTTP响应代码200),它就会一直成功。如果PHP端存在错误,则需要设置相应的错误标头,以便调用jQuery错误函数。
结论:您很可能不会向您的AJAX函数返回HTTP错误代码,导致始终调用成功函数。
答案 1 :(得分:0)
请勿使用前端语言来验证表单输入。
我看到对'request_tutor_process.php'的AJAX调用使用POST,所以request_tutor_process.php应该有类似的东西:
if($_POST['action'] == 'formSubmit'){
//Let's say you have this function called 'add_to_database'
attemptToAdd = add_to_database();
//Let's say something went wrong with the form validation, and you return 'false'
if(attemptToAdd === false) echo "Failed to submit, please try again later";
//Let's say it worked, but it was unsuccessful (the username already exists)
if(attemptToAdd === "unsuccessful") header('Location: /failed.php');
//Let's say it worked fully...
if(attemptToAdd === "successful") echo "It worked, click *here* to continue;
}
答案 2 :(得分:0)
你的问题是它总是会进入成功模块,对吗?
如果是这样,请关注@Zarathuztra answer并在您的PHP代码中将HTTP代码设置为200以外的任何值,并添加一个错误块来处理错误。 请参阅此答案Return errors from PHP run via. AJAX?
或者您可以检查数据的值,看看是否有错误。
答案 3 :(得分:0)
试试这个:
success: function (data) {
$("#loading-image").hide(); //hide loading
$("#result").html(data);
if(data == "success") {
setTimeout(function () {
$('input[type=submit]').attr('disabled', false);
window.location.href = "login/index.php";
}, 2000);
} else {
alert('error');
return false;
}
顺便说一句,PHP有点乱,但这不是问题。基本上因为数据有一个值,它正在成功,你没有处理错误