affichage jquery .serializeArray (); sortie par l'intermédiaire ajax
-
27-10-2019 - |
Question
Question rapide
Si je sérialisé un formulaire en utilisant la fonction .serializeArray();
de jquery dois-je faire quoi que ce soit à lui avant que je puisse l'envoyer à l'aide ajax data:
de jquery?
par exemple. puis-je envoyer
[{name: inp1, value: 'val1'}, {name: inp2, value: 'val2'}]
comme est, ou dois-je préprocesseur en quelque sorte?
et, en php comment pourrais-je lire?
La solution
It would be better here to use serialize
. This converts your form's values into a simple string that can be used as the AJAX call's data
attribute:
var myData = $('#yourForm').serialize();
// "inp1=val1&inp2=val2"
$.ajax({
url: "http://example.com",
data: myData
});
Presuming you send this to PHP using the GET
method, you can access these values using $_GET['inp1']
and $_GET['inp2']
Edit: You can convert an array made by serializeArray
into a parameter string using $.param
var myData = $('#yourForm').serializeArray();
// remove items from myData
$.ajax({
url: "http://example.com",
data: $.param(myData) // "inp1=val1&inp2=val2"
});