Associate
I'm not a PHP or JSON expert so I may be missing something obvious here, but I've got a problem with a PHP script returning JSON data. It works fine if I hard code a variable, but refuses to work when I use $_GET.
When I run the PHP script on its own, I get data as expected. The PHP code is (I've removed the SQL that populates the arrays for brevity):
When I run this script with '?pro=Test+Project' appended to the URL, the following is returned, which confirms the PHP script works with the $_GET variable:
However, when I try to retrieve the data using this javascript:
The following is returned:
However, if I change
for
it works OK. I'm pretty confused as the $_GET works fine when running the PHP, but not when retrieving via js. The fact a hard coded variable works suggests the js is OK so I'm well confused. Been trying to fix for hours and its properly bugging me now.
Can anyone advise?
Thanks.
When I run the PHP script on its own, I get data as expected. The PHP code is (I've removed the SQL that populates the arrays for brevity):
PHP:
<?php
// Get $_GET value
$projectName = $_GET['pro'];
// SQL REMOVED FOR BREVITY
$chartData = array();
array_push($chartData,$category);
array_push($chartData,$series);
print json_encode($chartData, JSON_NUMERIC_CHECK);
?>
When I run this script with '?pro=Test+Project' appended to the URL, the following is returned, which confirms the PHP script works with the $_GET variable:
Code:
[{"name":"Date","data":[2,3,4,5,6,7,8]},{"name":"Test Project","data":[3.3,null,0.2,null,null,null,null]}]
However, when I try to retrieve the data using this javascript:
Code:
$.getJSON("charts/projectWeekHours.php", function(json) {
options.xAxis.categories = json[0]['data'];
options.series[0] = json[1];
chart = new Highcharts.Chart(options);
});
The following is returned:
Code:
[{"name":"Date","data":[2,3,4,5,6,7,8]},{"data":[null,null,null,null,null,null,null]}]
However, if I change
PHP:
$projectName = $_GET['pro'];
for
PHP:
$projectName = 'Test Project';
it works OK. I'm pretty confused as the $_GET works fine when running the PHP, but not when retrieving via js. The fact a hard coded variable works suggests the js is OK so I'm well confused. Been trying to fix for hours and its properly bugging me now.
Can anyone advise?
Thanks.
Last edited: