[AJAX] Div recharge x secondes
Bonjour j ai un probleme j ai voulu creer un chat, celui ci fonctionne tres bien en php mais le probleme c est pour le rafraichir , comment dois je faire ? on m a dit de rafraichir ma div en ajax ttes les x secondes
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<div id="chat_contain">
<?php if($chat_array):?>
<table>
<?php foreach($chat_array as $chat):?>
<tr>
<td>
<span class="pseudo_chat"><a href="profile/index/<?php echo $chat['username'];?>">[<?php echo $chat['username'];?>]</a></span>
</td>
<td>
<?php $format = 'DATE_ISO8601';
$time = strtotime($chat['created_at']);
?>
<?php echo date('H:i',$time);?>
</td>
<td>
<?php echo $chat['content'];?>
</td>
</tr>
<?php endforeach;?>
</table>
<?php endif;?>
</div> |
J ai donc mis cela dans mon head :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <script language="javascript" type="text/javascript">
URL = "<?php $this->load->view('include/refresh_chat'); ?>";
function rafraichir() {
if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
else if (window.ActiveXObject) xhr = new ActiveXObject('Microsoft.XMLHTTP');
else alert('JavaScript : votre navigateur ne supporte pas les objets XMLHttpRequest...');
xhr.open('GET',URL,true);
xhr.onreadystatechange = ajaxReponse;
xhr.send(null);
}
function ajaxReponse() {
if (xhr.readyState == 4) {
document.getElementById("chat_contain",true).innerHTML=xhr.responseText; // ici sa s'incrute dans la div <div id="page"></div> mais peut etre <td id="page"> ... Ici c'est L'id l'important
var timer=setTimeout("rafraichir()",2000); // rafraichie toute les 25secs
}
}
</script> |
mais rien du tout ne s affiche dans ma fenetre de chat je ne comprends pas :(
quelqu un peux m aider ?
Merci