1

So, as I explained above, I would access (using PHP) an object that I've created using JS and then I've passed to .php by using Ajax. I've heard about json, it can be useful or can I work without that?
I'm linking my code:

    var data = {
     v_Post: document.getElementById("v_Post").value,
     x: document.getElementById("x").value,
     y: document.getElementById("y").value,

      }

    $.ajax({
      type: "POST",
      url: "http://localhost:80/mySite.it/scripts/script.php",
      data: "data=" + data,
      dataType: "html",
      success: function(v) {
        alert("Chiamata riuscita!!!");
          },
      error: function(){
        alert("Chiamata fallita!!!");
      }
    });
  });
});

I've tried to do this in php, but it seems not to work:

   $data = $_POST['data'];
   echo '<script>console.log($data.v_Post); </script>';
   echo $data.v_Post;
matthias_h
  • 11,356
  • 9
  • 22
  • 40
Antonio
  • 21
  • 5
  • I don't see the necessity of json with the ajax request. You could simply provide the data to `data`, and then in php all three keys would be accessable on the `$_POST` associative array – Taplar Apr 06 '20 at 17:12

2 Answers2

1

data is an object, which you are converting to a string ("[object Object]") so the actual data is being lost before you send it to the PHP.

Just pass jQuery the object itself, without converting it to a string:

data: data,

Then jQuery will use the standard Form Url encoding which PHP will decode automatically and use to populate $_POST.

<?php
    header("Content-Type: text/plain");
    echo $_POST['v_Post'];
?>

It will then be available in the JS:

success: function(response_data) {
     alert(response_data);
},
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

If you check, "data=" + data evaluates to data=[object Object], due to the way objects are coerced to strings when concatenated. So that's not what we want.

Indeed, JSON can help, as it forms a nice, uniform interface for complex data. Also, you don't want to specify parameter encoding yourself (data=... as a string), as jQuery can do it for you, if you just pass an object containing parameters. I've also taken the liberty to rename the parameter to payload, so we don't have two different things both named data and confusing the answer. You would use it this way:

$.ajax({
  ...
  data: {
    payload: JSON.stringify(data)
  },
  ...
});

and then in PHP,

$payload = json_decode($_POST['payload']);

We could also send JSON directly as a POST request body, not as a POST parameter, but PHP is not really nice about accessing that. You can see more here.

However, you don't have a complex data structure, you have three flat variables - and that can be transferred simply as three POST variables. In that case, this suffices:

$.ajax({
  ...
  data: data,
  ...
});

and in PHP:

$v_Post = $_POST['v_Post'];
$x = $_POST['x'];
$y = $_POST['y'];
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Why are you sending data encoded as JSON wrapped in a one dimensions Form Url Encoded packet? Just use one or the other. – Quentin Apr 06 '20 at 17:16
  • @Quentin: I believe the edit I wrote in the meantime addresses that. – Amadan Apr 06 '20 at 17:17