[英]codeigniter form validation return problems
I have making some form validation to my signup. 我已经对我的注册进行了一些表格验证。 Sofar I can get true or false in my $ajax success function, but cannot get it to returning part to prceed rest of code... Can somebody write a solution to this? Sofar我可以在我的$ ajax成功函数中得到对还是错,但是不能将其返回到剩余的代码中……有人可以为此解决方案吗?
function checkIfIsInDB(o, n){
var target_url = window.location.pathname + 'user/check_if_username_exist',
valid = '';
$.ajax({
type: "POST",
dataType: "json",
url: target_url,
data: {
username: o.val()
},
success:
function(json){
if(json['check']===false){
o.addClass( "ui-state-error" );
updateTips( n + " has been alredy taken." );
}
if(json['check']===true){
valid = true;
}
}
});
alert(valid); //return(valid); need for returning true or false
}
You're working asynchronously, so you need to put your 'proceeding code' in the success: callback part of the $.ajax() function. 您正在异步工作,因此需要在$ .ajax()函数的成功部分中添加“处理代码”:callback部分。
You've done it correctly with the failed check, you just need to do the same in the 您已通过失败的检查正确完成了此操作,只需在
if(json['check'] ===true){
//put your success code here - call another function perhaps
}
part. 部分。
Try moving the alert()
inside the success callback. 尝试在成功回调中移动alert()
。
function checkIfIsInDB(o, n) {
var target_url = window.location.pathname + 'user/check_if_username_exist',
valid = '';
$.ajax({
type: "POST",
dataType: "json",
url: target_url,
data: {
username: o.val()
},
success: function (json) {
if (json['check'] === false) {
o.addClass("ui-state-error");
updateTips(n + " has been alredy taken.");
}
if (json['check'] === true) {
valid = true;
}
alert(valid); //return(valid); need for returning true or false
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.