1

I would like to ask how to display error message when null value and 0 value in the ajax result from SQL

{"value":
    {"columns": [["More than 85%",null],["Less than 85%",0]],
    "type":"pie"}
}

else no pop out message shown.

$.ajax({
    type: "POST",
    url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
    dataType: "json", 
    success: function (result) { 
        var chart = c3.generate({
            bindto: '#piepie',
            data: result.value,
            color: { 
                pattern: ['#f35213', '#f1af4c'] 
            },
            pie: { title: "Productivity", }
        });     
    },
    error: function() {
        if ((result == null) && (result == 0)){ 
            alert ('Data are not ready yet!!');  
        } 
        else {
            ('error');
        }
    }   
});
0

2 Answers 2

1

The variable result doesn't exist in the error: function. You need to do that test in the success: function.

The null and 0 values are deep in the structure, you need to access them properly.

$.ajax({
    type: "POST",
    url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
    dataType: "json", 
    success: function (result) {
        if (result.value.columns[0][1] == null && result.value.columns[1][1] == 0) {
            alert ('Data are not ready yet!!');
        } else {
            var chart = c3.generate({
                bindto: '#piepie',
                data: result.value,
                color: { 
                    pattern: ['#f35213', '#f1af4c'] 
                },
                pie: { title: "Productivity", }
            });
        }
    },
    error: function() {
        alert('error');
    }   
});
Sign up to request clarification or add additional context in comments.

9 Comments

Are you sure the PHP script is returning json_encode(null) or json_encode(0) when there's no data available?
Maybe you should be testing if (result.value == null) instead of if (result == null)?
{"value":{"columns":[["More than 85%",null],["Less than 85%",0]],"type":"pie"}} the result is null and 0
That's result.value.columns[0][1] == null and result.value.columns[1][1] == 0.
{"percentage":["-100","0","-50","-50","0"],"new":"0","pending":"0","completed":"1","ongoing":"1","delay":"1"}
|
0

Try this,

$.ajax({
   type: "POST",
   url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
   dataType: "json", 
   success: function (result) { 
      if ((result == null) && (result == 0)){ 
        alert ('Data are not ready yet!!');  
      }else {
          var chart = c3.generate({
               bindto: '#piepie',
               data: result.value,
               color: { 
               pattern: ['#f35213', '#f1af4c'] },
               pie: { title: "Productivity", }
          });
       }        
   },
   error: function() {
      alert('error'); 
   }   
 });

1 Comment

can you post your response here ?? just want to see what response send by server and according to that i can suggest.

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.