Okay, so I've tried pretty much everything.
$.post("include/ajax.php", { type: "workbookNumber", wbn: $("input[name='wbn']").val() }, function(data) {
error.push("<li>"+data+"</li>");
alert(data);
});
The error.push is the error array that's been created, it works perfectly but it does not add to the array at all. It's as if that line of code does not exist. There have been instances in the comma variable data where there is an extra comma, showing it is there but even so, the <li></li> should still show.
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);
}
});
I can't think of any reasonable explanation for this, I was thinking that the PHP doesn't have enough time to show the result as it's searching through a database of over 20000 codes however, the result still comes back through the alert, just not through the error array in actual text that can be shown on screen.
The error array works fine, it's just this function that doesn't work. Here's some other examples of what DOES work correctly:
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>");
}
});
}
}
Here's the whole code:
$(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();
}
});
});
The var error is declared in your click() function and is thus not accessible outside that function. Declare it globally and your code should work. (Worked fine for me on jsfiddle with a global error variable.)
The rest of your error handling code works fine, because it is defined in the same scope as your error variable (the click() function). But the callback to your ajax request is not executed in the context of your function, but in the window context. This is definitely a scope issue.
Of course you have to wait for the response from the server to come back to update your error notifications. Write a function that iterates over the error array and displays the corresponding notifications and then call that function from the error function of your AJAX call.
Try the following:
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With