gpt4 book ai didi

php - 用于表单验证的 jQuery post 函数只允许 alert() 显示返回的数据

转载 作者:可可西里 更新时间:2023-11-01 01:06:02 25 4
gpt4 key购买 nike

好的,所以我已经尝试了几乎所有的方法。

$.post("include/ajax.php", { type: "workbookNumber", wbn: $("input[name='wbn']").val() }, function(data) {
error.push("<li>"+data+"</li>");
alert(data);
});

error.push 是已创建的错误数组,它运行良好,但根本不会添加到数组中。就好像那行代码不存在一样。在逗号变量数据中有一些实例,其中有一个额外的逗号,表明它在那里,但即便如此,<li></li>应该仍然显示。

jQuery.ajax({
type: "POST",
url: "include/ajax.php",
dataType:"html",
data: { type: "workbookNumber", wbn: $("input[name='wbn']").val() },
success:function(response) {
alert(response);
},
error:function (xhr, ajaxOptions, thrownError) {
alert("damn. -_-");
alert(xhr.status);
alert(thrownError);
}
});

我想不出任何合理的解释,我认为 PHP 没有足够的时间来显示结果,因为它正在搜索超过 20000 个代码的数据库,但是结果仍然通过警报,而不是通过可以在屏幕上显示的实际文本中的错误数组。

error数组没问题,就是这个函数不行。以下是一些其他正确工作的示例:

if($("input[name='fname']").val() == "") {
error.push("<li>The first name field is blank</li>");
}
if($("input[name='lname']").val() == "") {
error.push("<li>The last name field is blank</li>");
}

if($("select[name='usertype']").val() == 0) {
if($("input[name='vcode']").val() == "") {
error.push("<li>The voucher code field is blank</li>");
} else {
$.post("include/ajax.php", { type: "findVoucher", vcode: $("input[name='vcode']").val() }, function(data) {
if(data == "none") {
error.push("<li>The voucher code does not exist</li>");
}
});
}
}

完整代码如下:

$(document).ready(function() {
$("#sit_date").datepicker();
$("select[name='usertype']").change(function() {
if($(this).val()=="0") {
$(".indv").slideUp(500);
$(".comp").slideDown(500);
} else {
$(".indv").slideDown(500);
$(".comp").slideUp(500);
}
});
$("input[name='marka'],input[name='markb']").bind("keypress paste keyup blur focus", function() {
var marka = $("input[name='marka']").val();
var markb = $("input[name='markb']").val();
var perc = (marka/markb)*100;
if(perc>0 && perc<=100) {
$("#per").html(Math.round(perc));
} else {
$("#per").html("");
}
});
$("input[name='vcode']").bind("keypress keyup paste blur focus", function() {
$.post("include/ajax.php", { type: "checkVoucher", vcode: $(this).val() }, function(data) {
$("input[name='group']").val(data);
});
$.post("include/ajax.php", { type: "checkType", vcode: $(this).val() }, function(data) {
$("input[name='certificates']").val(data);
});
});

$("input[name='wbn']").bind("keypress keyup paste blur focus", function() {
$.post("include/ajax.php", { type: "getAssessment", wbn: $(this).val() }, function(data) {
if(data!="") {
$("select[name='assessment']").html(data);
}
});
});

/*
//turn into function
$(document).keyup(function(event){
if(event.keyCode == 13){
alert("works");
$("input[name='manual_add']").click();
}
});
*/

var error = [];
$("input[name='manual_add']").click(function() {
if($("input[name='fname']").val() == "") {
error.push("<li>The first name field is blank</li>");
}
if($("input[name='lname']").val() == "") {
error.push("<li>The last name field is blank</li>");
}

if($("select[name='usertype']").val() == 0) {
if($("input[name='vcode']").val() == "") {
error.push("<li>The voucher code field is blank</li>");
} else {
$.post("include/ajax.php", { type: "findVoucher", vcode: $("input[name='vcode']").val() }, function(data) {
if(data == "none") {
error.push("<li>The voucher code does not exist</li>");
}
});
}
}

if($("input[name='wbn']").val() == "") {
error.push("<li>The workbook number field is blank</li>");
} else {

$.post("include/ajax.php", { type: "workbookNumber", wbn: $("input[name='wbn']").val() }, function(data) {
error.push("<li>"+data+"</li>");
//this is the only thing that works correctly:
alert(data);
});

}

if(($("input[name='questions']").val() == "") && ($("input[name='marka']").val() != $("input[name='markb']").val())) {
error.push("<li>The questions wrong field is blank</li>");
}

var list = "";
$.each(error, function(i,val) {
list += val;
});

if(error.length>0) {
$(".error").slideUp(500);
$(".jquery-error ul").html("").append(list);
$(".jquery-error").slideDown(500);
} else {
$("form").submit();
}
});
});

最佳答案

var error 是在您的 click() 函数中声明的,因此在该函数之外无法访问。全局声明它,您的代码应该可以工作。 (在带有全局 error 变量的 jsfiddle 上对我来说效果很好。)

您的其余错误处理代码工作正常,因为它与您的 error 变量(click() 函数)定义在同一范围内。但是对 ajax 请求的回调不是在函数的上下文中执行的,而是在 window 上下文中执行的。这绝对是一个范围问题。

当然你必须等待服务器的响应回来更新你的错误通知。编写一个遍历 error 数组并显示相应通知的函数,然后从 AJAX 调用的 error 函数中调用该函数。

尝试以下操作:

var error = [];

jQuery.ajax({
type: "POST",
url: "include/ajax.php",
dataType:"html",
data: { type: "workbookNumber", wbn: $("input[name='wbn']").val() },
success:function(response) {
alert(response);
},
error:function (xhr, ajaxOptions, thrownError) {
error.push("<li>"+thrownError+"</li>");
showErrors();
}
});​

function showErrors () {
var list = "";
$.each(error, function(i,val) {
list += val;
});

if(error.length>0) {
$(".error").slideUp(500);
$(".jquery-error ul").html("").append(list);
$(".jquery-error").slideDown(500);
} else {
$("form").submit();
}
}

关于php - 用于表单验证的 jQuery post 函数只允许 alert() 显示返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10785801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com