Reputation: 49
Im trying to display an error message to saying that , When result is 0 from database (mysql) that will be an error message.
The code below im trying to use ajax to get SQL result and when it return 0 error will be display.
$.ajax({
type: "POST",
url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
dataType: "json", // serializes the form's elements.
success: function (result) {
var chart = c3.generate({
bindto: '#piepie',
data: result.value,
color: {
pattern: ['#f35213', '#f1af4c'] },
pie: { title: "Productivity", }
});
},
error: function() {
if (result.percentage==undefined){
alert ('Data are not ready yet!!');
} else {
alert(result.percentage);
}
}
});
Upvotes: 1
Views: 792
Reputation: 1241
Try this!
if ((typeof result !== " undefined") && (result !== null) && (result == 0)){
alert("YourErrorMessageHere");
}
Upvotes: 0
Reputation: 493
if (result.percentage.length==0){ // THIS IS WHAT YOU NEED.
alert ('ERROR!!');
} else {
// YOUR CODE GOES HERE.
}
As you have commented you are getting result as '{"percentage":[]}'. So you have percentage property as an array with length 0. So, you need to test that only.
Upvotes: 0
Reputation: 1851
$.ajax({
type: "POST",
url: "charts/prod.php?year=" + $("#selectyear").val() + "&month=" + $("#selectmonth").val(),
dataType: "json", // serializes the form's elements.
success: function (result) {
if (result.value == "0"){
alert ('ERROR!!');
} else {
var chart = c3.generate({
bindto: '#piepie',
data: result.value,
color: {
pattern: ['#f35213', '#f1af4c'] },
pie: { title: "Productivity", }
});
}
},
error: function() {
alert ('Error');
}
});
Try this
Upvotes: 0