jquery - Ajax within an each() method -
i have 3 scripts
the first 1 dynamically generates form contain several inputs values contain id_mensaje i'm intereted in. want delete mysql registries related id_mensaje. i'd rather show scripts
1.- correo.php
<div id='eliminar_mensaje'><a href='#'>eliminar</a> </div> <form> <input class='elim_msg' type='checkbox' value='id_mensaje_".$i."' /> ... more inputs whith same class name </form>
2.- correo.js
$('#eliminar_mensaje a').on('click', function(){ $('#loading').show(); $('.elim_msg').each(function(i,e){ var value = $(e).val(); var checked = $(e).prop('checked'); if(value != 'ok' && checked == true){ $.ajax({ url: 'private/control_correo.php', type: 'post', data: 'id_mensaje='+value+'&accion=eliminar_mensaje' }); } else { //do nothing } }); location.reload(true); return false; });
3.- control_correo.php
...} else if(isset($_post['accion']) , $_post['accion'] == 'eliminar_mensaje'){ consultav2("delete destinatarios id_mensaje = '".$_post['id_mensaje']."';"); consultav2("delete mensajes id_mensaje = '".$_post['id_mensaje']."';"); }
when click on link whithin eliminar_mensaje div looks javascript code works doesn't reason i'm not able find.
does of see in scripts?
tanks lot in advance!
you need success , error parameters in ajax call determine whether request successful or not:
$.ajax({ url: 'private/control_correo.php', type: 'post', data: 'id_mensaje='+value+'&accion=eliminar_mensaje', success: function(message) { // things on success alert(message); }, error: function(message) { // ajax request failed (not server side processing failed) alert(message); } });
you should @ building array of data pass first, passing in array , doing 1 ajax call.
$('#eliminar_mensaje a').on('click', function(){ $('#loading').show(); var ajaxarray = {}; $('.elim_msg').each(function(i,e){ var value = $(e).val(); var checked = $(e).prop('checked'); if(value != 'ok' && checked == true){ ajaxarray.push( e ); // add contents of e array } else { //do nothing } }); // done finding data, ajax $.ajax({ url: 'private/control_correo.php', type: 'post', data: { dataarray: ajaxarray }, success: function(message) { // things on success alert(message); }, error: function(message) { // ajax request failed (not server side processing failed) alert(message); } }); // server side script loops through passed array , things there, 1 ajax request });
also, what's location.reload() for? happens on click , might interfering ajax processing... if send array 1 request, can put location.reload success function.
Comments
Post a Comment