How to send and process N distinct forms with ajax without giving refresh on the page?

5

How to do it when you have n forms with the same ID , or CLASS send the data to process and when you finish processing change the color of <'tr bgcolor="#FFFF00"'> to green '#00FF00' signaling that the process has been completed. All this has to happen without giving refresh (without updating) on the page and using

asked by anonymous 30.05.2014 / 02:31

1 answer

10

First, you can not have multiple elements with the same id , as commented above @Andrey. Correct this in your HTML or it will be invalid (out of the problems you will have when trying to locate these repeated elements by id). You can replace the ids with classes.

Simply submitting all forms to Ajax, using the serialize method of jQuery, which scans a form and collects the values from the fields. The example below uses Promises to detect when everything is complete.

var promessas = [];
var url = "url_do_seu_php";
$("form").each(function(){
  var dados = $(this).serialize();
  promessas.push($.post(url, dados));
});
$.when.apply($, promessas).done(function(){
  // todos os formulários foram processados
});
    
30.05.2014 / 03:15