Existe-t-il un moyen d’utiliser javascript et JQuery pour append des champs supplémentaires à envoyer depuis un formulaire HTTP à l’aide de POST?
Je veux dire:
$("#form").submit( function(eventObj) { // I want to add a field "field" with value "value" here // to the POST data return true; }
Oui. Vous pouvez essayer avec des parameters cachés.
$("#form").submit( function(eventObj) { $('').attr('type', 'hidden') .attr('name', "something") .attr('value', "something") .appendTo('#form'); return true; });
Essaye ça:
$('#form').submit(function(eventObj) { $(this).append(' '); return true; });
Vous pouvez append une input
hidden
avec la valeur que vous devez envoyer:
$('#form').submit(function(eventObj) { $(this).append(''); return true; });
$('#form').append('');
Cela marche:
var form = $(this).closest('form'); form = form.serializeArray(); form = form.concat([ {name: "customer_id", value: window.username}, {name: "post_action", value: "Update Information"} ]); $.post('/change-user-details', form, function(d) { if (d.error) { alert("There was a problem updating your user details") } });