PHP script working but null data being returned when using $_GET variable

Associate
Joined
18 Oct 2002
Posts
2,055
Location
Southend-on-Sea
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):

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:
Associate
OP
Joined
18 Oct 2002
Posts
2,055
Location
Southend-on-Sea
Thanks Spunkey that fixes it. I knew it would be something obvious!!

I thought since the actual web page url that calls the javascript had a pro variable it would get used, but obviously I'll need to append it to the call to the php script.

Much appreciated.
 
Back
Top Bottom