JSON object via jQuery post for php

I know there are many questions, but none of them worked for me.

I create an array with normal javascript objects in javascript and sent it through jquery.postto the server. However, on the server, I cannot access the data using php $obj->value. I tried json_decode/encode, etc.

This is what console.log(data)gives me before sending it to the server.

enter image description here

Than in the php part I do this only:

 $data= $_POST['data'];
 print_r($data);

Print_r output:

enter image description here

And here is what my jQuery post looks like:

    $.post("programm_eintragen.php",{
            data: data,

        }).success(
            function(data){                 
                    //success

        }).error(
            function(){
            console.log("Error post ajax " );
        },'json');      

Can anyone tell me:

How can I access my object properties on php site correctly?

I also get tried to access non object ....or php interprets the json object as a string a data[0]returns to me [.

I thought I could do it like this:

$data[0]->uebungen[0] 

Am I just stupid and something is missing?

json php ​​?

+4
2

JavaScript, JSON, . JSON, () .

$.post("programm_eintragen.php",{
  data: JSON.stringify(data),
});

(php script) JSON. .

$data = json_decode($_POST['data'], true);

var_dump($data[0]['uebungen'][0]);

. json_encoding , . javascript .

$data = $_POST['data'];
var_dump($data[0]['uebungen'][0]);

+10

:

JavaScript

var data_obj = {
    "function": "create_customer",
};
$.post({ url: "src/index.php", dataType: "json", data: data_obj }, function (data) {

});

PHP

$json_obj = json_decode(json_encode($_POST));
$function = $json_obj->{"function"};

URL - index.html.

0

Source: https://habr.com/ru/post/1548247/


All Articles