0

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...

6
  • put a success and an error function in the ajax callback and see which 1 is hit and also please show the error messages from the network tab Commented Nov 11, 2017 at 13:31
  • Doesn't work how? Commented Nov 11, 2017 at 13:32
  • echo(json_encode("this".$data)); looks weird. What are you trying to accomplish there? Commented Nov 11, 2017 at 13:33
  • i try to send what php recieves Commented Nov 11, 2017 at 13:33
  • 2
    Then you should just do a 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). Commented Nov 11, 2017 at 13:36

1 Answer 1

1

If you try this code you will find that your php variable $data is an array()

$data = json_decode($_POST["data"]);
var_dump($data);

Then in the seconde line you have

echo(json_encode("this".$data));

whiche caused the error beacuase you can not concatenate string with an array "this".$data So try this simple changes.

echo("this".json_encode($data));

because json_encode($data) return a string than you can concatenate it with "this" wich is string

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.