I want to submit a form using Ajax but I want it to be in an array, not serialized.
JavaScript
x
var form = $(this);
data: {data:form.serialize()};
gives me this in my PHP:
JavaScript
firstname=John&lastname=Doe&phone=123123412345
and I have to run this code in PHP to get it into the format I want:
JavaScript
parse_str($_POST['data'], $data);
Just seems super unnecessary and I would like to remove that step. How can I just pass the form in an array format? Thanks
Advertisement
Answer
JavaScript
// var form = $(this);
var form = this;
data: {data: Object.fromEntries(new FormData(form))}
then you will get the $_POST
variable like this:
JavaScript
Array
(
[data] => Array
(
[firstname] => John
[lastname] => Doe
[phone] => 123123412345
)
)