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);
1
  • This question addresses how to use cookies with JavaScript. Make sure you're not passing any sensitive info around with JavaScript Commented Nov 29, 2015 at 0:21

2 Answers 2

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.

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

1 Comment

Thanks, but when I get in php I only get "{" should use a for or something?. I'm not so sure.
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 :)

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.