0

I am currently designing a PHP script / AJAX that allows me to do monitoring, in fact on my intranet I want to retrieve a list of actions that have been stored in the database in the day. So I made a request for the moment I try to view the member asynchronously but it does not work.

In fact I wanted to display the members for testing and after adapting to recover agendas for the actions of this day and asynchronously (without reload) because I have to be able to observe the recorded data in real time.

So I did this code:

In script.js is in the header I put this:

function writeInDiv(text){
    var objet = document.getElementById('monitoring');
    objet.innerHTML = text;
}

function ajax()
{
    var xhr=null;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("GET", "ajax.php", false);
    xhr.send(null);
    writeInDiv(xhr.responseText);
        setInterval("ajax()",5000);
}

Then I have two other pages, namely that displays the code, it contains the following:

<button onclick='javascript:ajax()'>Afficher</button>
                <div id="monitoring"></div>


<?php

$requette = mysql_query("SELECT * FROM gestionnaire ORDER BY ID DESC LIMIT 0,10");
$result=mysql_query($requette) or die;
While($donne = mysql_fetch_array($requette))
{
$message = htmlentities($donne['nom']);
$pseudo = htmlentities($donne['prenom']);
?>
<b><?php var_dump($donnee); echo $pseudo; ?>:</b> <?php echo $message; ?><br />
<?php
}
?>

the trouble is that nothing appears I have not even an error message, though the table is filled with these fields I tried a var_dump but in case like this, it is nothing.

I do not know, or it may come from.

1 Answer 1

3

Ajax calls are asynchronous, maybe the call didn't finish at the moment you pass the responseText to writeInDiv. Try the following:

function writeInDiv(text){
    var objet = document.getElementById('monitoring');
    objet.innerHTML = text;
}

function ajax()
{
    var xhr=null;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("GET", "ajax.php", false);
    xhr.send(null);
    xhr.onreadystatechange=function()
    {
        if (xhr.readyState==4 && xhr.status==200)
        {
             writeInDiv(xhr.responseText);
        }
    }
    setTimeout("ajax()",5000);
}

or with jQuery:

function ajax() {
    $('#monitoring').load('ajax.php');
    setTimeout("ajax()",5000);
}

Also, I think you misspelled var_dump($donnee); it may be var_dump($donne);

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

Comments

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.