2

I have a PHP function that returns a value or a blank page and I want to check it with JS.

PHP:

function get_CTDIvolAVG($min, $max) {
    $query = "SELECT AVG(ct.ctdivol_mGy) FROM exam 
              INNER JOIN ct on exam.examID = ct.examID AND ct.ctdivol_mGy > 0 
              WHERE exam.modality = 'CT' 
              AND (unix_timestamp(exam.date)*1000) >= '" . $min . "' 
              AND (unix_timestamp(exam.date) * 1000) < '" . $max ."';";

    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    $line = mysql_fetch_array($result, MYSQL_ASSOC);

    echo $line["AVG(ct.ctdivol_mGy)"];
}

JS:

function afterSetExtremes(e) {
  var chart = $('#container').highcharts();
  $.getJSON('http://localhost/public_html/webservice.php/CT/CTDIvol/AVG/' + Math.round(e.min) +
            '/' + Math.round(e.max), function (AVG) {

    if (AVG) {

      chart.yAxis[0].removePlotLine('avg');
      chart.yAxis[0].addPlotLine({
      value: AVG,
      color: 'green',
      width: 2,
      id: 'avg',
      dashStyle: 'longdashdot',
      label: {
        text: 'CT Average: '  + AVG.toFixed(2)}
      });       
    } else {
      console.log('blank');
    }       
  });
}

When PHP code returns nothing, it should enter the else clause in JS and print 'blank' but it doesn't happen.

How can I fix this?

1
  • And if you console.log(AVG) what do you get Commented Jan 29, 2016 at 14:27

1 Answer 1

1

The problem is in the event of a SQL error, or no results are being returned by the query, there is no JSON string for your code to work with.

In that case, the code inside function(AVG) {...} does not execute. This is the reason you aren't getting anything when you console.log(AVG) and your console.log('blank') statement doesn't fire.

To fix this, make sure your PHP is always generating a JSON string, even for errors, and that you are parsing the JSON appropriately in the javascript.

Here is an example, you can use this to confirm that your PHP is returning a JSON string.

function testJSON(json) {
    console.log('testing '+json+'...');
    var jqxhr = $.getJSON( json, function() {
    console.log( "success" );
  })
    .done(function() {
      console.log( "second success" );
    })
    .fail(function() {
      console.log( "error" );
    })
    .always(function() {
      console.log('finished testing '+json+'!');
    });
 }


 //this one has a valid JSON
 testJSON('https://jsonplaceholder.typicode.com/users');

 //this does not have a JSON string, and would not enter your function(AVG) code
 testJSON('https://jsonplaceholder.typicode.com/');

You can see it working here: https://jsfiddle.net/zq6ww3s9/

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @Adam Konieska ! I've followed your suggestions and fixed it by adding a json_encode in the echo clause. Now, the get_CTDIvolAVG function always return a string and I can check it with the way I've posted (if (AVG) {}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.