JakaStudent7
JakaStudent7

Reputation: 21

AJAX: How to receive boolean value from php

I have a questions and I was searching on StackOverFlow, but for some reasons the examples I found didn't work correctly.

I have a following Ajax script (I just sending 2 variables to php file to check if they are in an array):

    $.ajax({ url: 'info.php',
         data: {'row':row, 'col':col},
         type: 'GET',
        dataType: 'json',
         success:  function(){
              console.log(row + col);
});

But the thing is that I don't know how to properly send (PHP) data back to "Ajax" and how to capture it:

I have been trying with:

   echo json_encode($Result);  //Where $Result is a boolean

The data is sent correctly from ajax to .php file, I recieve a status 200 and if I go to .php file I can also see that the data was properly recieved.

I was using information from here: How to get true or false from PHP function using AJAX?

And here: ajax sucess: i can not check if the returned information is true or false

I have tried implementations from both cases. I would appreciate any tips.

Upvotes: 0

Views: 635

Answers (1)

Aaron Clover
Aaron Clover

Reputation: 11

There are a few things I would check in your situation - first is, as you've already checked, making sure the response you're getting is valid. As it's 200, we can rule that out.

The second thing I'd check is that your data is coming back in the proper json format, because when you specify "datatype: 'json'" you're telling the ajax call to "treat this as a json object", meaning if it's malformed you'll actually receive an error.

Try adding an error function to your ajax object:

$.ajax({
    url: 'info.php',
    data: {'row': row, 'col': col},
    type: 'GET',
    dataType: 'json',
    success:  function() {
        console.log(row + col);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(errorThrown);
    }
});

That will at least tell you whether or not the output is coming back correctly.

If you still don't get output after that, it's possible that console.log is being tied up by a library you're using, so maybe try an alert() instead. Other than that we might need more information, so tell me what happens once the error function is in place.

Upvotes: 1

Related Questions