0

I want to draw a google chart, the query is written in a function in ajax.php file. The chart is in different php file calling the json data using ajax.

I want to call a function in php file, joblocation().

function joblocation(){

global $DB;

//query by location total post
$sql = 'SELECT ljl.location_id, count(ljj.job_id) as count, ljl.location_name FROM {local_jobs_job} ljj INNER JOIN {local_jobs_location} ljl ON ljj.job_location = ljl.location_id WHERE ljj.job_type = 0 GROUP BY ljl.location_name';


//get the query into record
$data = $DB->get_records_sql($sql);

//put the query into array
$rows = array();

$rows = array_map(function($item) {
return (object) ['c' => [
    (object) ['v' => $item->location_name, 'f' => null],
    (object) ['v' => intval($item->count), 'f' => null]
]];
}, array_values($data));


 // prepare return data
$cols = [
(object) ['id' => '', 'label' => 'LABEL', 'pattern' => '', 'type' => 'string'],
(object) ['id' => '', 'label' => 'TOTAL', 'pattern' => '', 'type' => 'number'],
];

$returndata = new stdClass;
$returndata->cols = $cols;
$returndata->rows = $rows;

echo json_encode($returndata);
}

This is the ajax call to call the function.

 var jsonColumnChartData = $.ajax({
                            url: "ajax.php",
                            contentType: "application/json",
                            data: {action: 'joblocation'},
                            dataType: "json",
                            async: false
                            }).responseText;

Is it correct to do this way?

I tried this:

var jsonPieChartData = $.ajax({
                            url: "ajax.php",
                            contentType: "application/json",
                            data: {},
                            dataType: "json",
                            async: false,
                            success: function(result){
                                joblocation(result);
                                },
                            error: function() {
                                alert('Error occured');
                                }
                            }).responseText;

When I run, the error pop up appear. It doesn't get the function.

joun
  • 656
  • 1
  • 8
  • 25
  • You want a `success: function(results) { ... }` in the $.ajax properties to get the returned data. https://stackoverflow.com/questions/3302702/jquery-return-value-using-ajax-result-on-success for example. – John Hascall Sep 12 '17 at 02:47
  • Do I need to put isset statement in ajax.php file? – joun Sep 12 '17 at 03:19
  • No idea. I don't do php. Recommend you use a browser which can debug Ajax calls (e.g. Chrome) and see what error you are getting. One possible issue is the `async: false` -- synchronous Ajax is "considered harmful" these days – John Hascall Sep 12 '17 at 07:09
  • ok..thanks @John Hascall. Hope someone else can help me. – joun Sep 12 '17 at 07:52

0 Answers0