Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    Ajax e refresh .........

    Ciao.
    Ho questo semplice script che
    serve per recuperare una pagina
    php con una query.
    Codice PHP:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    script language="JavaScript" type="text/javascript">
        var 
    time 2000
        var 
    page "query.php";
        
    //Gets the browser specific XmlHttpRequest Object
        
    function getXmlHttpRequestObject() {
            var 
    receiveReq=false//Clear our fetching variable
            
    try {
                    
    receiveReq = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
            
    } catch (e) {
                    try {
                            
    receiveReq = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
                
    } catch (e) {
                    
    receiveReq false;
                            }
            }
            if (!
    receiveReq && typeof XMLHttpRequest!='undefined') {
                   
    receiveReq = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
            
    }
            return 
    receiveReq;
        }            
        
    //Get our browser specific XmlHttpRequest object.
        
    var receiveReq getXmlHttpRequestObject();        
        
    //Initiate the asyncronous request.
        
    function getQuery() {
            
    //If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
            
    if (receiveReq.readyState == || receiveReq.readyState == 0) {
                
    //Setup the connection as a GET call to SayHello.html.
                //True explicity sets the request to asyncronous (default).
                
    receiveReq.open('GET'page true);
                
    //Set the function that will be called when the XmlHttpRequest objects state changes.
                
    receiveReq.onreadystatechange handlegetQuery
                
    //Make the actual request.
                
    receiveReq.send(null);
    alert("Sono ingetQuery");
                
    setTimeout('getQuery()',time);
            }            
        }
        
    //Called every time our XmlHttpRequest objects state changes.
        
    function handlegetQuery() {
            
    //Check to see if the XmlHttpRequests state is finished.
    alert("Sono in handlegetQuery");
            if (
    receiveReq.readyState == 4) {
                
    //Set the contents of our span element to the result of the asyncronous call.
                
    var content receiveReq.responseText;
                
    document.getElementById('news').innerHTML content;
                }
        }
    window.onload = function()
    {
        
    getQuery();
    }
    </script>
        </head>
        <body>
            
            <p id="news"></p>
            
        </body>
    </html> 
    Il risultato della query lo
    recupera senza battere ciglio.
    Il problema è che vorrei non so
    ad un nuovo inserimento o update
    del Db vedere l'aggiornamento senza
    fare il refresh della pagina per questo
    ho provato ad utiizzare setTimeout ma
    non funziona dov'è che sbaglio ?




    Ps.
    Per la cronaca vince handlegetQuery 3-1
    Dato che ci siamo se mi spiegate anche questa
    mi immagino che sia per i vari stati readyState
    ma vorrei conferma o smentita
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  2. #2

    ..............

    Uppete
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    io non è che abbia capito molto...
    comunque, se voglio fare una richiesta ajax a tempo, farei circa così:

    window.setTimeout("richiesta_ajax()", 1000);
    Pietro

  4. #4

    ..........

    Ti ringrazio del suggerimento
    ma in questo modo viene richiamata
    una volta sola.
    Codice PHP:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    script language="JavaScript" type="text/javascript">
        var 
    time 2000
        var 
    page "query.php";
        
    //Gets the browser specific XmlHttpRequest Object
        
    function getXmlHttpRequestObject() {
            var 
    receiveReq=false//Clear our fetching variable
            
    try {
                    
    receiveReq = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
            
    } catch (e) {
                    try {
                            
    receiveReq = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
                
    } catch (e) {
                    
    receiveReq false;
                            }
            }
            if (!
    receiveReq && typeof XMLHttpRequest!='undefined') {
                   
    receiveReq = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
            
    }
            return 
    receiveReq;
        }            
        
    //Get our browser specific XmlHttpRequest object.
        
    var receiveReq getXmlHttpRequestObject();        
        
    //Initiate the asyncronous request.
        
    function getQuery() {
            
    //If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
            
    if (receiveReq.readyState == || receiveReq.readyState == 0) {
                
    //Setup the connection as a GET call to SayHello.html.
                //True explicity sets the request to asyncronous (default).
                
    receiveReq.open('GET'page true);
                
    //Set the function that will be called when the XmlHttpRequest objects state changes.
                
    receiveReq.onreadystatechange handlegetQuery
                
    //Make the actual request.
    alert("pippo");
                
    receiveReq.send(null);
            }            
        }
        
    //Called every time our XmlHttpRequest objects state changes.
        
    function handlegetQuery() {
            
    //Check to see if the XmlHttpRequests state is finished.
            
    if (receiveReq.readyState == 4) {
                
    //Set the contents of our span element to the result of the asyncronous call.
                
    var content receiveReq.responseText;
                
    document.getElementById('news').innerHTML content;
                }
        }
    window.onload = function()
    {
        
    window.setTimeout("getQuery()"1000); ;
    }
    </script>
        </head>
        <body>
            
            <p id="news"></p>
            
        </body>
    </html> 
    L'intento sarebbe ottenere una sorta di 'refresh'
    (mi sto chiedendo se sia possible una cosa del genere
    e dove sto facendo uno sbaglio a livello concettuale)
    Detto in due parole apro la pagina e se ad esempio l'ammistratore
    del DB o chi per lui effettua un INSERT o un UPDATE senza
    effettuare nessun tipo di operazione (lascio la finestra aperta)
    vedere questi INSERT o UPDATE una sorta di real time.
    Spero di essermi spiegato meglio.
    Fammi sapere.

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  5. #5
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116

    Re: ..........

    Originariamente inviato da whisher
    Ti ringrazio del suggerimento
    ma in questo modo viene richiamata
    una volta sola.
    tu hai una funzione del tipo:

    ajax(url, parametri, onload,...)

    il parametro onload è la funzione di callback ed è lì che deve essere lanciato il setTimeout...
    Pietro

  6. #6
    ciao!
    Non so se ho capito bene bene bene... comunque.
    Non è che tu vuoi usare setInterval()...
    In questo modo ogni tot millisecondi dichiarati ti chiama la funzione...
    Occhio però, io avevo fatto una cosa del genere per un miniportale e queste continue richieste erano pesantucce sia a livelli di banda occupata sia a livello di calcolo...
    ciao,

    mrBinding

  7. #7
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    solo per fare un esempio, una pagina recupera un dato e controlla periodicamente il dato tramite richista ajax. Direttamente da Access modifico il dato, e praticamente in tempo reale vedo le modifiche nella pagina

    il codice è pressapoco così:
    codice:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="controllo database.aspx.vb" Inherits="CorsoApogeo_ajax_semplice_ajax_controllo_database" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Pagina senza titolo</title>
        <link href="../../../stili/Styles.css" rel="stylesheet" type="text/css" />
    
        <script type="text/javascript" src="../../../js/sajax.js"></script>
    
        <script language="javascript" type="text/javascript">
    // <!CDATA[
    window.onload = function()
    {
        controlla_database();
    }
    
    function controlla_database()
    {
        sajax("?comando_ajax=controlla_database", onload);
        function onload()
        {
            $("div1").innerHTML = this.request.responseText;
            $("div2").innerHTML = $("div1").innerHTML;
            window.setTimeout(controlla_database,1000);
        }
    }
    
    // ]]>
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            Modificare il campo TESTO per id=1 della tabella campi, direttamente da Access e vedere il risultato con chiamate ajax continue
            <hr />
            <div id="div1"></div>
            <hr />
            <div id="div2"></div>
        </form>
    </body>
    </html>
    Pietro

  8. #8
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    dimenticavo, per me, l'uso di ajax in questo modo, è un puro suicidio (ma sono per la libertà di azione )

    Pietro

  9. #9

    ............

    dimenticavo, per me, l'uso di ajax in questo modo, è un puro suicidio (ma sono per la libertà di azione
    Posso essere d'accordo.
    Ma tu nell'esempio usi
    sajax("?comando_ajax=controlla_database", onload);
    sajax (ci devo dare un occhio )nell'esempio che ho postato prima
    la cosa non funziona e non riesco ancora a capire cosa ci sia
    di sbagliato (indipendentemente dal fatto che sia un modo appropriato di usare
    ajax ).





    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  10. #10
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116

    Re: ............

    Originariamente inviato da whisher
    Posso essere d'accordo.
    Ma tu nell'esempio usi

    sajax (ci devo dare un occhio )nell'esempio che ho postato prima
    la cosa non funziona e non riesco ancora a capire cosa ci sia
    di sbagliato (indipendentemente dal fatto che sia un modo appropriato di usare
    ajax ).

    per sajax intendo una funzione da me fatta, banalissima, che non ho spedito.
    Quello era solo un esempio per spiegare come strutturare una funzione ajax

    AJAX(URL, PARAMETRI, FUNZIONEDIRITORNO, ALTRO...

    come ho detto, metti setTimeout dentro la funzione di ritorno

    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.