Right now I have a php file that recieves a string from a search bar on page1 of my website. It queries the data bases and creates a JSON string of results. I can then use PHP to redirect my website to page2 (The search results page). How can I receive that JSON string in the Javascript of page2?
Asked
Active
Viewed 4,453 times
0
-
Please [edit in](http://stackoverflow.com/posts/20186455/edit) your current code. – Dave Chen Nov 25 '13 at 07:03
-
I am just looking for a general method at the moment – Karaja Nov 25 '13 at 07:15
-
http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – MeNa Nov 25 '13 at 07:19
3 Answers
0
First you need a PHP file that will encode json code . Make a file like this:
http://localhost/project/myjs.php
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
Then your JavaScript (in this example I use jQuery):
$.getJSON('http://localhost/project/myjs.php', function(data) {
console.log(data);
});
This should be a start to get PHP arrays in to your JavaScript.

Mahmood Rehman
- 4,303
- 7
- 39
- 76
0
To redirect browser to another page you have to use header
function
header:('Location: http://example.com/page2.php');
You can store data in $_SESSION
array between pages like that:
page1.php
session_start();
$_SESSION['array'] = array('foo'=>$bar);
page2.php
session_start();
echo json_encode($_SESSION['array']);

ailinykh
- 73
- 1
- 5
0
page1.php:
<?php
$my_array = array('foo'=>$bar);
$json_data = json_encode($my_array);
header:('Location: http://example.com/page2.php?data='.$json_data);
?>
page2.php
<?php
$data_get = $_REQUEST['data'];
?>
<script type="text/javascript">
var mydata =<?php echo $data_get; ?>;
</script>

user3011768
- 191
- 2
- 3
- 11