Announcement

Collapse
No announcement yet.

continue reading database and updating table on site

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • continue reading database and updating table on site

    I was wondering if it is possible to get a real time update from a database. Meaning that if the database gets a new entry while the site is already loaded have the new entry show in the table on the site? It's for a simple message board I usually have the client reload the whole page to get the newest posts but I want to have it done automatically, any suggestions?

  • #2
    Hi,

    A bit late in responding but if you're still watching the thread.

    You'd need to use "AJAX" for this as well as a server side lingo.

    JavaScript AJAX implementation
    Code:
    function getRTUpdate()
    {
        var http = new XMLHttpRequest();
    var url = "page.php"; //this page should be scripted to return the correct data.
    var params = "lorem=ipsum&name=binny"; // parameters to be passed.
    http.open("POST", url, true);
    
    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    
    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText); // this is the data returned - people usually use JSON.
            returnData = JSON.Serialize(http.responseText); // makes it a readable array if using JSON.
        }
    }
    http.send(params);
    }
    JQuery Implementation - I'm not quite as proficient in Jquery as i am in vanilla Javascript = you may need to do further research on this.
    Code:
    $.ajax({
        url:"getvalue.php",  
        success:function(data) {
          handleData(data); //do your fancy stuff with the returned data here.
        }
      });
    And then a basic server side page to return the data to the AJAX request

    ajax.php
    Code:
    ?php
    //conection:
    $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
    
    //consultation:
    
    $query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));
    
    //execute the query.
    
    $result = $link->query($query);
    
    //display information:
    $ret = "";
    while($row = mysqli_fetch_array($result)) {
      $ret .= $row["name"] . "<br>";
    }
    
    echo $ret;
    ?>
    Who needs a Signature?

    Comment

    Working...
    X