Reputation: 1689
I have a JavaScript array which contains JSON pairs of values of the form:
var myArray = [{attribute: "attributeID1", option: "optionID1"},
{attribute: "attributeID2", option: "optionID2"}];
I have a PHP script which wants to take that data in array form and search my database with it. I am not looking to remain on the page while this search is happening so I don't want to use AJAX - I just want to pass this data to the PHP script which will then render the new page.
What is the easiest way for me to do this?
Upvotes: 0
Views: 512
Reputation: 834
First, you'll need to grab a copy of json2.js so that you can convert your object into a JSON string.
https://github.com/douglascrockford/JSON-js
You can then stringify your object and place it in a hidden input field in the form.
document.getElementById('search').value = JSON.stringify(your_object);
Now that will be sent over the to PHP script where you can decode it using the built in function json_decode()
http://php.net/manual/en/function.json-decode.php
$object = json_decode($_POST['search']);
Upvotes: 1
Reputation: 16210
Put it in a hidden input
element. Then submit the form that the input element is in.
Upvotes: 1