1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| var map = new Map([
[ "id1", { idDiv: "reponse1", nameFile: "ajax_guide.php" } ],
[ "nomForm", { idDiv: "cible", nameFile: "ajax_article.php" } ],
// etc.
]);
$(document).submit(function (event) {
event.preventDefault();
var form = event.target;
var params = map.get(form.id);
console.log(params);
var $div = $("#" + params.idDiv);
$(document).ajaxStart(function () {
$div.css({ "color": "black" });
$div.text("Transaction en cours
");
});
var donnees = $(form).serialize();
$.ajax({
method : "POST",
url : params.nameFile,
data : donnees,
dataType : "text",
success : function (data) {
$div.css({ "color": "red" });
$div.text(data);
},
error : function () {
$div.css({ "color": "red" });
$div.text("Une erreur est survenue");
}
});
}); |
Partager