Help me please: I create my object:
var data = [];
$("#report-container [id^='report-']").each(function(index) {
var reportObject = {
"subject" : "",
"photo" : "",
"rating" : "",
"comment" : ""
};
reportObject.subject = $("#name-report-"+index).text();
reportObject.photo = $("input[name='subject-photo-"+index+"']")[0].files[0];
reportObject.rating = $("input[name='subject-rating-"+index+"']").val();
reportObject.comment = $("textarea[name='subject-comment-"+index+"']").val();
data.push(reportObject);
});
After this I have an array. I convert it to json like this:
var myarray = JSON.stringify(data);
if I console log it, it looks like this:
[{"subject":"Окна","rating":"0","comment":""},{"subject":"Пол","rating":"0","comment":""}]
And then I send it to php:
$.ajax({
type: "POST",
url: "/report-data/add_report.php",
data: { data: myarray },
async: true,
cache: false,
and in php I try to get it like this:
$data = json_decode($_POST["data"]);
echo(json_encode("this".$data));
And it doesn't works...
echo(json_encode("this".$data));looks weird. What are you trying to accomplish there?var_dump($_POST)and check the real response in the dev consoles network tab. You're actually changing the data with that json_encode() (since you're adding things to it).