When I need to pass along data to another page, I use some JavaScript to:
- create a form element,
- add a named input element to the form,
- convert data to JSON and store in the input,
- submit the form via post to the new page.
Here's my helper function:
function submitJSON( path, data, postName ) {
// convert data to JSON
var dataJSON = JSON.stringify(data);
// create the form
var form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', path);
// create hidden input containing JSON and add to form
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", postName);
hiddenField.setAttribute("value", dataJSON);
form.appendChild(hiddenField);
// add form to body and submit
document.body.appendChild(form);
form.submit();
}
Use it like this:
var myData = {param1: 1, param2: 2);
var myPath = "path_to_next_page.html";
submitJSON( myPath, myData, 'myPostName' );
Retrieve the data on the next page like this:
<?php
$postVarsJSON = $_POST['myPostName'];
$myData = json_decode( $postVarsJSON );
?>
Or in JavaScript:
var myData = JSON.parse( <?php $_POST['myPostName']; ?>);