Issues with $.post() please help,
I have 2 db tables:
manufacturers (id, manufacturer_name)
madels (id, model_name, manufacturer_id)
I want to give the option to delete items in the DB. But when they delete a manufacturer I want to run an alert saying "Deleting this Manufacturer will also remove their models" so I don't have unassociated data in my models table. Makes sense right?
To note, please ignore any general syntax and spelling errors that my have occurred in writing this. The code works great except for the single issue, so any programming errors to that point are because I've minimized the code to try and be a little more straightforward.
General comments are in red
Error comments are in dark red
function delete_item(e, get_confirmation) {
e.preventDefault(); //The click event is fired from a "Delete" link
var post_url;
var what = $(e.target).attr('href'); //The ID of what needs to be deleted is in the link href
var where = $(e.target).parents('div').attr('id'); //The name of the table that the ID is located is found in the id="" of the parent div wrapped around the delete links
if(get_confirmation == 'unconfirmed') { //I attach a click event to the delete link that calls this function sending "get_confirmation===unconfirmed" making this statement true
post_url = "http://.../delete.php?confirmation=false";
$.post(post_url, {table: where, item: what}, function(data) { // This sends the pieces of data in $_POST variables to a page that is also looking for a $_GET['confirmation'];
if(data == 'exists') { // The page.php first checks to see if models exist under this specific manufacturer's ID. It "echo exists;" if a model exists (no problems with my code so far)
confirmation = confirm("Deleting this Manufacturer will also delete all associated models. Continue?"); // Get the confirmation variable, make sure the user really wants to do this.
delete_item(e, confirmation); // <-- Here is where the problem lies
} else if(data == 'true') { //This gets called if there aren't any models under the manufacturer's ID (if this is true, it deletes perfectly)
load_url = "http://.../reload_data.php"; //A page that loads only the new data
$('#manufacturers').load(load_url, function() { // Refresh the data section of the page (essentially a reload without having to reload)
$('#manufacturers a.delete').click(function(e) { delete_item(e, 'unconfirmed'); }); //Reapply the "click" event to the delete links to call this function with the "unconfirmed" variable
});
}
});
} else if(get_confirmation == true) { //Ok, so if someone confirms that they really do want to delete this manufacturer and it's associated models
post_url = "http://.../delete.php?confirmation=true";
$.post(post_url, {table: where, item: what}, function(data) {
// This sends the pieces of data in $_POST variables to a page that is also looking for a $_GET['confirmation'];
// Since confirmation=true, the page.php knows that if it's gotten this far it knows that models already exist for this manufacturer. So go and delete everything. Here is where I get stuck. The $.post function works to this point, looking at the database everything gets deleted. However, I can't get into this function to reset the view. I can't even put an "alert(data)" to post anything. Meaning everything happens to this point, but never lets me get INTO this function.
// So question, is this breaking because I am re-calling the delete_item function from within a $.post() function's success function? Can jquery do that?
// If not, can I get some help figuring out what to do instead?
} else { //This is called if "get_confirmation" == false
alert("Well why did you click delete then?");
}
}