samedi 2 juillet 2016

How to get a variable from Ajax with Javascript


Underneath i have added my ajax script named checkMessages.php: $user = $system->getUserInfo($_SESSION['user_id']); $checkmes = $db->query("SELECT * FROM notifications WHERE receiver_id='".$user."' AND sent='0'"); $checkmes = $checkmes->fetch_object(); if(!empty($checkmes)) { $checkmes = true; } else{ $checkmes = false; } And also the javascript im trying to create: function checkMessages() { $.get("'.$system->getDomain().'/ajax/checkMessages.php, function(data){ $checkmes }); if $checkmes == true { refreshChat(); } } window.setInterval(function(){ checkMessages(); }, 1000); What i am trying to do is the following. checkMessages.php checks if there is a new message and i have made a setinterval to execute the function checkMessages() and call the ajax script every second. So if there is a new message, the variable $checkmes is set to true. And if it is true, javascript should execute a different function, refreshChat(). But my question is, how can i get the variable $checkmes from checkMessages.php with javascript, have it read if it is true? I have been searching high and low but i have no idea. I've recently started to learn about coding so i hope someone can teach me how to do this. I would like to thank everybody who helps out beforehand.

Aucun commentaire:

Enregistrer un commentaire