Pass a list of objects from JavaScript to PHP

2

My code is as follows:

function actualizarEstado(){
hi = Object.values(actualizaciones);
window.location = ('actualizar.php?actualizaciones='+ hi);


}
<?php
$actualizaciones= $_REQUEST['actualizaciones'];
print_r($actualizaciones);
?>

Updates is a variable containing a list of objects where stablesco 2 id (iduser and idselect) then to insert in utilzo php database.

But print the following: [object Object], [object Object]

    
asked by Diego Lopez 10.11.2017 в 16:00
source

1 answer

5

If actualizaciones is an object of type

{
 id_user: 1,
 id_select:2
}

When you concatenate it with actualizar.php?actualizaciones= you are casting it to a String. That cast will always give you [object Object]

What you could do to get started (although it's not the right way in reality) is to convert your object to a string using JSON.stringify and, on the server side, convert it back to an object (or array) using json_decode .

function actualizarEstado(){
  hi = Object.values(actualizaciones);
  window.location = ('actualizar.php?actualizaciones='+ JSON.stringify(hi));
}

<?php
  $actualizaciones= $_REQUEST['actualizaciones'];
  print_r(json_decode($actualizaciones,true));
?>

But again, this is not the right way to do it. Depending on the values in your arrangement, you would have to sanitize what you pass to the URL where you redirect. There are a thousand things that can go wrong with this approach to the problem ... but if you're just experimenting, I guess this answer will suffice to push you in the right direction.

Only as a suggestion: It would be easier for you if you pass the content of actualizaciones through Ajax.

    
answered by 10.11.2017 / 16:09
source