0

Attempting to run a scheduled alert with jquery and JSON. The php file works as expected and returns a correctly JSON encoded array. The jquery works and pops up a dialog box every minute when not checking for the JSON data. But when i add in the JSON code, it stops working and nothing happens.

Disclaimer: I know I should no longer be using mysql and should be using prepared statements instead, and I will begin as soon as this project is finished.

EDIT I fixed the typo and changed $getJSON to $.getJSON, and i posted the php script results.

jQuery:

$(function() {

setInterval(function() {

    $.getJSON('includes/popup.php', function(data) {

        if(data.firstName == "None") {

            $("<div>No Callback</div>").dialog();   

        }
        else {

            $("<div>Yes Callback</div>").dialog();

        }

    })

}, 1000 * 60 * 1);

});

popup.php

<?php

$nowdate = date("Y-m-d");
$nowtime = date("H:i");

$alertSql = "SELECT id, compid, cbdate, cbtime, firstName, lastName FROM contacts WHERE  cbdate = '$nowdate' ORDER BY lastName desc";
$alertResult = mysql_query($alertSql, $link);

while($alertRow = mysql_fetch_array($alertResult)){

    $cbtime = date("H:i", strtotime($alertRow['cbtime']));

    if($cbtime == $nowtime) {

        $final_array[] = array("firstName" => $alertRow['firstName'], "lastName" =>                 $alertRow['lastName'], "cbdate" => $alertRow['cbdate'], "cbtime" => $alertRow['cbtime']);

        echo json_encode($final_array);
    }
    else {

        $final_array[] = array("firstName" => "None");

        echo json_encode($final_array);

    }

};

?>

php result:

[{"firstName":"None"}]

 or 

[{"firstName":"Mickey","lastName":"Mouse","cbdate":"2014-09-22","cbtime":"15:36:00"}]
2
  • 1
    Can you try console.log(data); in callback function & post your result Commented Sep 22, 2014 at 15:18
  • What does data look like in the response? Can you paste the exact PHP response? Commented Sep 22, 2014 at 15:18

3 Answers 3

2

There's a typo:

$getJSON(...)

should be

$.getJSON(...)

The Javascript console should have alerted you to the undefined function.

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

1 Comment

Oh! I can't believe I didn't see that! Anyway, I fixed it, and it still isnt producing the dialog box. It should be producing the "No Callback" one every minute, but nothing is happening
2

This line:

$getJSON('includes/popup.php', function(data) {

Should be;

$.getJSON('includes/popup.php', function(data) {

1 Comment

Oh! I can't believe I didn't see that! Anyway, I fixed it, and it still isnt producing the dialog box. It should be producing the "No Callback" one every minute, but nothing is happening
0

you have to convert the json into a javascript object before you use it. Change your code to the below snippet:

$.getJSON('includes/popup.php', function(data) {
  var myobj = jQuery.parseJSON(data);
  if(myobj.firstName == "None") {

    $("<div>No Callback</div>").dialog();   

  }
  else {

    $("<div>Yes Callback</div>").dialog();

  }
})

1 Comment

No, getJSON() parses the data before passing it onto the callback function. See, for example, the examples in the docs that don't do any explicit parsing.

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.