A
A
Anatoly2021-07-24 08:39:19
PHP
Anatoly, 2021-07-24 08:39:19

How to extract an object from a form?

Through ajax I pass the form, which has 2 elements:

...
let form = new FormData();
form.append('id', '1');

let data = {"name":"John", "sex":"male", "age":"33"};
form.append('data', data);
...


How to retrieve an object in php?
var_dump($_POST["id"]);
// string(1) "1"
var_dump($_POST["data"]);
// string(15) "[object Object]"
var_dump($_POST["data"]["sex"]);
// string(1) "["
var_dump($_POST["data"] -> sex);
// NULL NULL

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-07-24
@Tolly

You cannot pass an object inside form.append () as the second parameter: you can only pass a string or only a file, but not an object! To send objects to the server, you need to use JSON. And by the way, in your example, there is not enough code responsible for submitting the form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question