2

I'm stuck trying to pass an object to another page , using javascript. I hope you can give me a hand . Thanks.

Here the object created and sent through a form and input.

var cars = {nombre : "luis", apellido: "heya"};

var form = '<form action="RegisterOrder.php" method="post">'+'<input type="hidden" name="arrayDatosProductos" value="'+cars+'"></input>'+'</form>';
$(form).submit();

In the page that receives the object :

var a = new Object(<?php echo $_REQUEST['arrayDatosProductos']; ?>);
alert(a.nombre);
SilverWolf
  • 51
  • 5
  • This [question](http://stackoverflow.com/questions/2257631/how-create-a-session-using-javascript) addresses how to use cookies with JavaScript. Make sure you're not passing any sensitive info around with JavaScript – salad_bar_breath Nov 29 '15 at 00:21

2 Answers2

3

JavaScript

var cars = {nombre : "luis", apellido: "heya"};

var form = '<form action="RegisterOrder.php" method="post">'
    + '<input type="hidden" name="arrayDatosProductos" value="'
    + String( JSON.stringify(cars) )
    + '"></form>';
$(form).submit();

PHP

var a = <?= $_REQUEST['arrayDatosProductos']; ?>;
alert(a.nombre);

I am not sure about the jQuery stuff, submitting a form created in JavaScript, but you need to convert the object cars to JSON and have it echoed in PHP into the new variable.

Hasse Björk
  • 1,431
  • 13
  • 19
1

Well, after several attempts I could solve my problem , the last was by quotation marks (""), well, I stay this way :

var cars = {nombre : "luis", apellido: "heya"};
var ll = JSON.stringify(cars);

var form = '<form action="RegisterOrder.php" method="post">'
+'<input type="text" name="arrayDatosProductos" value='
+String(ll)+'></input>'+'</form>';

$(form).submit();

Y por ultimo:

var a = <?php echo $_REQUEST['arrayDatosProductos']; ?>;
alert(a.nombre);

I hope it will help someone in the future :)

SilverWolf
  • 51
  • 5