/    Sign up×
Community /Pin to ProfileBookmark

Attention to all expert coders – why the **** does this not work?!

Just the part that says ‘this works, but this doesn’t’! Don’t understand why the alert gives the data back (so the AJAX is fine) but the error handling doesn’t work properly. Any suggestions?

[code=html]$(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);
});
});

/*
//fix for fast typing/duplicate results with time() as request number
var latestRequestNumber = 0;
$.ajax({
type: “POST”,
url: “include/ajax.php”,
data: {company : serchval, requestNumber: ++latestRequestNumber},
success: function(data) {
var results = (JSON.parse(data));
if (results.requestNumber < latestRequestNumber) return;
// … otherwise, display results from this response …
}
});
*/

$(“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);
}
});
});

$(“input[name=’manual_add’]”).click(function() {
var list = “”;
var error=0;
if($(“input[name=’fname’]”).val() == “”) {
list = list + “<li>The first name field is blank</li>”;
error++;
}
if($(“input[name=’lname’]”).val() == “”) {
list = list + “<li>The last name field is blank</li>”;
error++;
}

var wbn = $(“input[name=’wbn’]”).val();
if(wbn == “”) {
list = list + “<li>The workbook number field is blank</li>”;
error++;
} else {
$.post(“include/ajax.php”, { type: “workbookNumber”, wbn: wbn }, function(data) {
alert(“This result works: ” + data);
list = list + “<li>This result doesn’t work:” + data + “</li>”;
error++;
/*
if(data==”duplicate”) {
$(“.alert”).slideDown(500);
} else {
list = list + “<li>” + data + “</li>”;
error++;
}
*/
});
}

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

/*
if($(“input[name=’wbn’]”).val() != “”) {
list = list + “<li>The workbook number field is blank</li>”;
error++;
} else {
$.post(“include/ajax.php”, { type: “checkUsed”, wbn: $(“input[name=’wbn’]”).val() }, function(data) {
alert(data);
list = list + “<li>”+data+”</li>”;
error++;
if(data == “none”) {
list = list + “<li>The workbook number does not exist</li>”;
error++;
}
var arr = data.split(‘/’);

//problem here?
if(arr[0] != “0” && arr[2] != “0000-00-00 00:00:00”) {

list = list + “<li>The workbook code has already ben used.</li>”;
error++;
$.post(“include/ajax.php”, { type: “checkNames”, userid: arr[0], type: arr[1], fname: $(“input[name=’fname’]”).val(), lname: $(“input[name=’lname’]”).val() }, function(data) {
if(data == “”) {
list = list + “<li>The workbook code has already ben used.</li>”;
error++;
if(arr[1] == “0”) {
list = list + “<li>This workbook number has been used by an online assessment user. If this is another attempt, please ensure the first and last names are correct.</li>”;
error++;
}
if(arr[1] == “1”) {
list = list + “<li>This workbook number has already been used by a manual assessment user. If this is another attempt, please ensure the first and last names are correct.</li>”;
error++;
}
} else {
// work out what you’re gonna do with the duplicate
// this workbook number has already been used, is this a duplicate or not? popup window thing… maybe if error==0, then test to see if the user exists with the same name and ask for duplicate with different box…
$(“.alert”).slideDown(500);
}
});
}
});
}
*/

if(($(“input[name=’questions’]”).val() == “”) && ($(“input[name=’marka’]”).val() != $(“input[name=’markb’]”).val())) {
list = list + “<li>The questions wrong field is blank</li>”;
error++;
}

if(error>0) {
$(“.error”).slideUp(500);
$(“.jquery-error ul”).html(“”).append(list);
$(“.jquery-error”).slideDown(500);
} else {
$(“form”).submit();
}
});
});[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@omnicityMay 25.2012 — You say that this does not work properly - can you elaborate on what does and doesn't happen, and what you expect it to do?
Copy linkTweet thisAlerts:
@dangerousprinceauthorMay 25.2012 — Where the alert shows the error inside the post function, and the list gets added to the variable, basically the alert shows the data but the list and error variables do not work at all. It's as if they're not there.
Copy linkTweet thisAlerts:
@omnicityMay 28.2012 — I'm not convinced that your 'list' variable is in the same scope when you try to add to it, as when you try to read it - try Alert()ing that and check that it isn't a local/shadow variable.
×

Success!

Help @dangerousprince spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...