After I've done a MySQL operation I have the following lines of code (I've tried each one of them):
if($done){
echo json_encode("done");
flush();
}
if($done){
echo "done";
}
and in the Javascript side I have:
done(function(data) {
if(data){
alert(data);
console.log(data=="done");
Although the data appears correctly in the alert, the "console.log" always displays false. I've tried without the " " and with ' " " ' and what else but it just won't work. Any idea on why?
Based on what was said in the comments, you have whitespace surrounding the data
value. Trim off that whitespace with the trim()
function (available on up-to-date browsers):
console.log(data.trim()=="data");
In the first part of you're PHP code you've used json encode to put the php output into a javascript object but you haven't parsed this using JSON.parse() in your js.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Perhaps just use the PHP code that just echos the text:
if($done){
echo "done";
}
Also you can output a JS object to the console as follows:
console.log("%O", data);