javascript - Ajax call duplicated each time -
i have page form. form dinamically loaded jquery.load in document.ready loop.
$('#storico').load('bp/storico.php?az=<?php echo $id; ?>');
i have created delegated event handle form submission:
$(document).on('click','#update',function(e){ e.preventdefault(); var att_stor = $('#attivo_storico').val(); var pass_stor = $('#passivo_storico').val(); var az = <?php echo $id; ?>; $.ajax({ type: "post", url: "bp/storico1.php", data: {attivo_storico: att_stor, passivo_storico: pass_stor, azione: az}, success: function(data){ data1=data.split("|"); if(data1[0].trim() == "successo"){ $("#storico").empty().html('<center><p class="success">'+data1[1]+'</p></center>').animate({opacity:1}, 'slow'); }else if(data1[0].trim() == "errore"){ $("#storico").empty().html('<center><p class="err">'+data1[1]+'</p></center>').animate({opacity:1}, 'slow'); } }, complete: settimeout(function(){$('.container').load('bp/businessplan.php?id=<?php echo $id; ?>');},3000) }); });
the ajax call works charm , following:
- submit form , update db (no issues);
- update div containing form confirmation message (no issue)
- reload container div update whole page new data (used elsewhere in page)
the first loop works flawlessy. second time call executed twice, third 4 times, 8 times. if load else in container , go page execution starts again one. issue doesn't affect how scripts work i'd understand why duplicates calls (as firebug shows me)
thanks in advance!
try using:
$(document).off().on('click','#update',function(e){ e.preventdefault(); var att_stor = $('#attivo_storico').val(); var pass_stor = $('#passivo_storico').val(); var az = <?php echo $id; ?>; $.ajax({ type: "post", url: "bp/storico1.php", data: {attivo_storico: att_stor, passivo_storico: pass_stor, azione: az}, success: function(data){ data1=data.split("|"); if(data1[0].trim() == "successo"){ $("#storico").empty().html('<center><p class="success">'+data1[1]+'</p></center>').animate({opacity:1}, 'slow'); }else if(data1[0].trim() == "errore"){ $("#storico").empty().html('<center><p class="err">'+data1[1]+'</p></center>').animate({opacity:1}, 'slow'); } }, complete: settimeout(function(){$('.container').load('bp/businessplan.php?id=<?php echo $id; ?>');},3000) }); });
Comments
Post a Comment