Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript PHP JSON array
I'm trying to pass a json_encoded array from php (which reads mysql and produces an array) to javascipt so I can use the array as input to a java chart. The problem is that the array is fine in the php part (I run nthe php separately and use echo and print_r to check the contents and is all fine). Immediately after the php code I have javascript which always gets an empty array.
I used several different attempts, all recommended as solutions on various forums and none of them work. Putting quotes around the call to get the array just echoes the command to my variable, anything else just returns empty.
My code (snip) looks like...
 
// Using json_encode puts the data into an array format that we can use in a javascript
$amJSONArray = json_encode($amData);
 
// Echo is for debugging only.
// echo $amJSONArray;
 

var chartData = "<?php print($amJSONArray); ?>";  // This just returns the literal in the speech marks
var chartData = '<?php print($amJSONArray); ?>';  // This also returns the literal in the speech marks
var chartData = <?php echo $amJSONArray ?>;       // This returns empty
var chartData = (<?php echo $amJSONArray ?>);     // This returns empty
alert(chartData);                                                                   // Returns empty - just showing the contents of the array if I do the json_encode within the php part
alert(<?php echo $amJSONArray ?>);                              // Returns empty - just showing the contents of the arra if I do the json_encode during the array fetch
Posted 21-Apr-13 9:44am
Edited 22-Apr-13 11:01am
v3
Comments
Prasad Khandekar at 22-Apr-13 16:07pm
   
Please have a look at this doc (http://php.net/manual/en/function.json-encode.php).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
if you want to achieve this function,you need to use ajax.
your php side(a.php)
	$amData=array(1,2,3,4);
	$amJSONArray = json_encode($amData);
	echo $amJSONArray;
?>
your js side(b.html)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
	$(function(){
	      $.getJSON("a.php",function(result){
		  $.each(result,function(i,j){
			alert(j);//1,2,3,4
		  });
	      });
	})
</script>
try this,you will get the array from php side
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If PHP is returning JSON, this should work
 
<script>
.
.
.
i = eval("(" + amJSONArray + ")");
.
.
.
</script>
  Permalink  
Comments
enhzflep at 23-Apr-13 13:20pm
   
Yup, JSON.parse is a better option though - avoids so much of the horror that eval can unleash.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 290
1 Jochen Arndt 165
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100