Hola,
He conseguido actualizar constantemente un archivo php mediante ajax sin necesidad de refrescar toda la página con el siguiente código:
Código .js:
Código PHP:
/*
* Parametros mandatorios
*/
var seconds = 1; // el tiempo en que se refresca
var divid = "refrescar_entrenos"; // el div que quieres actualizar!
var url = "web/salas/sala_entrenos.php"; // el archivo que ira en el div
function refreshdiv(){
// The XMLHttpRequest object
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Tu explorador no soporta AJAX.");
return false;
}
}
}
// Timestamp for preventing IE caching the GET request
var timestamp = parseInt(new Date().getTime().toString().substring(0, 10));
var nocacheurl = url+"?t="+timestamp;
// The code...
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState== 4 && xmlHttp.readyState != null){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}
// Empieza la función de refrescar
window.onload = function(){
refreshdiv(); // corremos inmediatamente la funcion
}
En el head de mi html:
Código HTML:
<script type="text/javascript" src="web/js/refrescar_entrenos.js"></script>
Y finalmente, en el body, pongo esto:
Código HTML:
<div id="refrescar_entrenos">
<div name="timediv" id="timediv">
<?php include('web/salas/sala_entrenos.php'); ?>
</div>
</div>
Con esto consigo que me actualice el archivo sala_entrenos.php constantemente (es lo que quiero) pero me surge un problema. ¿cómo puedo hacer para que, en lugar de que me refresque un sólo archivo: sala_entrenos.php, me refresque 3 archivos:
sala_entrenos.php
sala_libres.php
sala_oficial.php
Gracias.