show automatically records

Discussion in 'JavaScript' started by piropeator, Sep 7, 2009.

  1. #1
    I want that when the database will add a record my window automatically displays the new records.

    What's missing?


    index.html
    ========
    <script language="javascript" src="ajax.js"></script>

    <script language="javascript">
    http = getXMLHTTPRequest();

    function generalista(){
    var myurl = 'cliente_lista.php';
    myRand= parseInt(Math.random()*99999999999999999);
    var modurl = myurl + "?rand=" + myRand;
    http.open("GET", modurl, true);
    http.onreadystatechange = useHTTPResponse;
    http.send(null);
    }

    function useHTTPResponse() {
    if (http.readyState == 4) {
    if(http.status == 200) {
    var miTexto = http.responseText;
    document.getElementById('listacliente').innerHTML = (miTexto);
    }
    } else {
    document.getElementById('listacliente').innerHTML='<img src="cargando.gif">';
    }
    }

    </script>
    <body onLoad="generalista()">
    <table width="500" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td> <div id="listacliente"></div></td>
    </tr>
    </table>
    </body>

    cliente_lista.php
    ============
    <?
    $cn=mysql_connect("localhost","user_db","key_bd");
    mysql_select_db("data_cliente");
    $sql="SELECT * FROM cliente";
    $rs=mysql_query($sql);
    $n=mysql_num_rows($rs);
    ?>
    <table width='400' border='1' cellspacing='0' cellpadding='0'>
    <tr>
    <td bgcolor= '#FFFFCC'>NOMBRES</td>
    <td bgcolor= '#FFFFCC'>TELEFONO</td>
    <td bgcolor= '#FFFFCC'>FICHA</td>
    <td></td>
    </tr>
    <? for($k=0;$k<$n;$k++){
    $idcliente = mysql_result($rs,$k,"idcliente");
    ?>
    <tr>
    <td> <? echo mysql_result($rs,$k,"nombres") ?> </td>
    <td> <? echo mysql_result($rs,$k,"telefono") ?> </td>
    <td> <img src="explorar.jpg"/></a></td>
    </tr>
    <? } ?>
    </table>

    Thats all my code.:eek:
     
    piropeator, Sep 7, 2009 IP
  2. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Do you want your page reloaded when somebody else insert a row in your db?

    Well, that is almost impossible. Because HTTP events come from the client, not the server. Let alone PUSH and other mechanism available for a small group of client/servers under certain circumstances.

    You can us a poll strategy instead. You can setup a timer in javascript which will reload table information via AJAX and update your HTML page.
     
    caprichoso, Sep 7, 2009 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    Well, and any idea how to do that? :)
     
    piropeator, Sep 17, 2009 IP
  4. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #4
    caprichoso, Sep 17, 2009 IP
  5. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #5
    piropeator, Sep 17, 2009 IP
  6. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #6
    You can put your AJAX request inside a timer.

    Use setInterval() javascript function for dispatching the request over a certain period of time.
     
    caprichoso, Sep 17, 2009 IP