php - jQuery .get() function returns an error -


i have jquery code:

$("#delete_products").click(function() {     $(":checkbox:checked").each(function() {         var pid = $(this).val();         $.get('delete_product.php',{pid: pid}); }); location.reload(); }); 

for reason, $.get() function doesn't work. when run .fail(), tell me has failed.

here delete_product.php page:

if(isset($_get['pid']) && !empty($_get['pid']) && is_numeric($_get['pid'])){     $pid = $_get['pid'];     $query = "delete `products` `id` = $pid limit 1";     $query_run = mysql_query($query) or die(mysql_error());     $query = "delete `products2categories` `product_id` = $pid";     $query_run = mysql_query($query) or die(mysql_error());     $pictodelete = ("../products_images/$pid.jpg");     if(file_exists($pictodelete)){         unlink($pictodelete);        }     header("location: index.php");     exit(); } 

any idea why happening?

it's not working because you're calling location.reload before request finished. cancels requests if haven't been returned. if want page reload afterwards, try this:

$("#delete_products").click(function () {     var promises = [];     $(":checkbox:checked").each(function () {         var pid = $(this).val();         promises.push($.get('delete_product.php', {             pid: pid         }));     });     $.when.apply($, promises).done(function () {         location.reload();     });     return false; }); 

for more information happening, take @ jquery documentation $.deferred: http://api.jquery.com/category/deferred-object/


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -