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

    [AJAX+PHP] eseguire alert javascript

    Salve a tutti,
    non riesco a risolvere un problema stupido con ajax...
    Vi spiego:
    Ho una pagina html che richiede dei dati ad una pagina php (azione.php) ogni due secondi..
    questo è il codice:
    codice:
    <script>
    function refreshDate(myUrl,myField){
    var Field = document.getElementById(myField); // selects the given element
    
    	if(window.XMLHttpRequest) {
    		xmlhttp = new XMLHttpRequest(); // Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera, Internet Explorer 7    
    	}
    	
    	if(window.ActiveXObject) {
    		try{        
    			xmlhttp = new ActiveXObject("MSXML2.XMLHTTP"); // Internet Explorer 6     
    			} catch(e) { 
    			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 4,5    
    			}    
    	}    
    
    		xmlhttp.open("GET", myUrl+ '?rnd=' + Math.random(), true);    
    		xmlhttp.onreadystatechange=function() {
    			if (xmlhttp.readyState == 4) {             
    				Field.innerHTML = xmlhttp.responseText; // puts the result into the element
    				     
    			}    
    		}    
    		xmlhttp.send(null); 
    		
    }
    </script>
    <body onLoad="refreshDate('azione.php','richieste');setInterval('refreshDate(\'azione.php\',\'richieste\');',2000);">
    <p id="richieste"></p>
    </body>
    e questo è il codice della pagina php:
    codice:
    <?php
    echo "ciao: ".time()."
    ";
    echo "<script>alert('funziona');</script>";
    
    ?>
    In teoria dovrei ricevere l'alert ogni due secondi...
    In pratica ricevo solo la scritta "ciao" + il tempo "time()"...
    Perchè non mi funziona?
    Grazie a tutti in anticipo!
    What is the |\/|atrix?

  2. #2
    Nessuna idea?
    What is the |\/|atrix?

  3. #3
    a parte che va spostato in scripting
    a parte che l'oggetto xmlhttp lo creerei con un else

    credo che ti serve setTimeout

  4. #4
    Utente di HTML.it L'avatar di chris
    Registrato dal
    Sep 1999
    Messaggi
    1,568
    http://forum.html.it/forum/showthrea...hreadid=911261
    http://ajaxpatterns.org/On-Demand_Javascript

    Può rimanere anche qui, anche se in scripting ci starebbe meglio.
    "Nei prossimi tre anni col mio governo vogliamo vincere anche il cancro, che colpisce ogni anno 250.000 italiani e riguarda quasi due milioni di nostri concittadini"

  5. #5
    tutto sull' onload, usi 2 globali (sono il male, ricordalo )

    <body onload="action = 'azione.php'; request = 'richieste'; refreshDate(action, request); setInterval('refreshDate(action, request);', 2000);">



    o anche così
    codice:
    function refreshDate(action, request) {
    	// operazioni che devi fare ...
    	setTimeout("refreshDate(rDobject.action, rDobject.request);", 2000);
    };
    var rDobject = {action:"azione.php", request:"richieste"};
    ... e poi
    <body onload="refreshDate(rDobject.action, rDobject.request);">
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  6. #6
    Grazie innanzitutto per le risposte...
    Ho provato a fare come diceva andr3a va non funziona, continua a stamparmi solo la scritta "ciao" + il tempo, e non ne vuole a che sapere per quanto concerne l'esecuzione dello script alert('funziona');
    Questo è il codice che ho utilizzato (seguendo le istruzioni di andr3a):
    codice:
    <script>
    function refreshDate(myUrl,myField){
    var Field = document.getElementById(myField); // selects the given element
    
    	if(window.XMLHttpRequest) {
    		xmlhttp = new XMLHttpRequest(); // Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera, Internet Explorer 7    
    	}
    	
    	if(window.ActiveXObject) {
    		try{        
    			xmlhttp = new ActiveXObject("MSXML2.XMLHTTP"); // Internet Explorer 6     
    			} catch(e) { 
    			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 4,5    
    			}    
    	}    
    
    		xmlhttp.open("GET", myUrl+ '?rnd=' + Math.random(), true);    
    		xmlhttp.onreadystatechange=function() {
    			if (xmlhttp.readyState == 4) {             
    				Field.innerHTML = xmlhttp.responseText; // puts the result into the element
    				     
    			}    
    		}    
    		xmlhttp.send(null); 
    		
    }
    </script>
    
    <body onload="myUrl = 'azione.php'; myField = 'richieste'; refreshDate(myUrl, myField); setInterval('refreshDate(myUrl, myField);', 2000);">
    <p id="richieste"></p>
    </body>
    Vi ringrazio ancora per l'aiuto!
    What is the |\/|atrix?

  7. #7
    up
    What is the |\/|atrix?

  8. #8
    andr3a...
    What is the |\/|atrix?

  9. #9
    Vi ringrazio lo stesso... ho risolto qui: http://microformats.org/wiki/rest/ahah
    Ciao!
    What is the |\/|atrix?

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.