No this is not another question about how to use AJAX.
Why does this not work for me ?
What the code should do:
-
When a button is clicked, it should send a Title and a User id to a PHP file, which should insert that in the database.
-
Then it should make a
.eachon all elements inside a div, and send each of them to a PHP file, which should insert them to the database. -
and finally it should redirect.
I get the first alert with “first” but i don’t get the one with “second” and nothing is inserted in the database.
My previous code worked fine with inserting, and it does the same, just not as advanced and without the redirect function
my code:
$('#tabCreateButton').click(function () {
alert("first"); //checking for click
$.ajax({
type: "POST", url: "/page/newtab.php", data: "action=newtitle&tabname=" + tabname + "&bid=" + brugerid,
complete: function (data) {
alert("second"); //checking for response
var tabid = $(data.responseText);
var XHRs = [];
$('#tabCurrentFriends > .dragFriend').each(function () {
var friendid = $(this).data('rowid');
XHRs.push($.ajax({
type: "POST", url: "page/newtab.php", data: "action=newtab&tabid=" + tabid + "&bid=" + brugerid + "&fid=" + friendid,
complete: function (data) {
}
}));
});
$.when(XHRs).then(function () {
alert("third"); //should redirect here.
});
}
});
});
You either have an XHR error or a Javascript error you are not seeing.
You can check for these errors using Chrome’s built in debugger (F12 –> Network –> XHR) or Firebug.