GrumpyCrouton
GrumpyCrouton

Reputation: 8621

AJAX request with JSON return is not responding with my expected behavior

I am attempting to write an ajax call that checks against my database and updates a div if it finds changes.

I'm relatively new to JavaScript, and even more new to JSON things.

I think the problem is in how I am returning the JSON array, or how it is used in the if statement, but I'm not sure how to do it properly. I came to that conclusion based on the fact I get a console log return from the success function "Connected and executed PHP page...", but NOT the "Updated Tickets Page..." log.

Here is my JavaScript:

function checkUpdates()
{
  $.ajax({
    type: "POST",
    url: 'ajax/checkDB.php',
    data: {},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
      console.log('Connected and executed PHP page...');
      if (data.hasChanged == true) {
        console.log('Updated Tickets Page...');
        $("#contents").load("dynamic/tickets.php");
        $("#contents").fadeTo("fast", 1);
      }
    }
  });
}

$(document).ready(function() {
  setInterval("checkUpdates()", 3000);  // Calls the function every 3 seconds
});

And here is my PHP (checkDB.php, called in the ajax request), if it matters.

<?php
        include("../static/config.php");
        session_start();

        header("Content-Type: text/json");

        $result = mysqli_query($con,"SELECT notify FROM tickets WHERE notify='0'");
        $row_cnt = mysqli_num_rows($result);

        if($row_cnt > 0) {
            echo json_encode(array('hasChanged' => 'true'));
            mysqli_query($con,"UPDATE tickets SET notify='1' WHERE notify='0'");
        } else {
            echo json_encode(array('hasChanged' => 'false'));
        }


?>

Upvotes: 2

Views: 67

Answers (3)

Igor Scabini
Igor Scabini

Reputation: 121

in the snipped of code you are checking if the string 'true' is equal to the boolean true.

You have two choice:

return a boolean from php with

echo json_encode(array('hasChanged' => true));

and

echo json_encode(array('hasChanged' => false));

or you can check string in javascript with:

if (data.hasChanged == "true") {

Upvotes: 2

Parth Trivedi
Parth Trivedi

Reputation: 3832

You are having typo error. You can change either php code without string or change if condition.

//changes made here
if (data.hasChanged == "true") {

PHP

if($row_cnt > 0) {
            //here you are having string "true"
            echo json_encode(array('hasChanged' => 'true'));
            mysqli_query($con,"UPDATE tickets SET notify='1' WHERE notify='0'");
        } else {
            echo json_encode(array('hasChanged' => 'false'));
        }

Upvotes: 2

Cihan Uygun
Cihan Uygun

Reputation: 2138

You should return your data in proper data type.

When you encode json with the code in your question, which 'true' is not a boolean so your javascript could not compare the 'hasChanged' property as a boolean.

Could you try the code below ? That will make 'hasChanged' property boolean.

echo json_encode(array('hasChanged' => true));

Upvotes: 0

Related Questions