Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/09/2007, 12:12
Avatar de ZiTAL
ZiTAL
 
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 2 meses
Puntos: 62
MiniChat con PHP/AJAX

Bueno estaba aburrido y he hecho un mini-chat en php/ajax, es muy facil de comprender y bueno esta bien para empezar con esto del Ajax:

ajax.js
Código:
function ajaxFunction01()
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    return xmlHttp;
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      return xmlHttp;
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        return xmlHttp;
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
  } 
function ajaxFunction02()
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    return xmlHttp;
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      return xmlHttp;
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        return xmlHttp;
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
  }
index.php
Código PHP:
<?php
if($_GET["Bota"]=="bai")
 {
  
$file fopen("file.txt""a");
  
fwrite($file,date("Y/m/d - H:i:s")." ".$_REQUEST["text"]."\n");
  
fclose($file);
  exit();
 }
elseif(
$_GET["Leidu"]=="bai")
 {
  
$file fopen("file.txt""r");
  while (!
feof($file)) 
   {
    
$buffer fgets($file4096);
    echo 
$buffer."<br />";
   }
  
fclose($file);
  exit();
 }
?>
Código HTML:
<html>
<head>
<title>Txat-tsue :)</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript"> 
function fajax()
{
    var text = document.getElementById('text').value;
    var ajax=ajaxFunction01();
    ajax.open("POST","?Bota=bai",true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("text="+text);
    document.getElementById('text').value="";
    document.getElementById('text').focus();
    fajax2();
}
function fajax2()
 {
    var ajax=ajaxFunction02();
    ajax.onreadystatechange=function()
      {
      if(ajax.readyState==4)
        {
        document.getElementById('txat').innerHTML=ajax.responseText;
        }
      else
       {
        document.getElementById('txat').innerHTML="kargaten...";
       }
      }
    ajax.open("GET","?Leidu=bai",true);
    ajax.send(null);    
 } 
setInterval("fajax2()",500);
</script>
</head>
<body>
<input type="text" id="text" size="50" maxlength="50" />
<input type="button" value="bota" onclick="fajax()" />
<div id="txat">
</div>
<script type="text/javascript">
    document.getElementById('text').value="";
    document.getElementById('text').focus();
 fajax2();
</script>
</body>
</html> 
Bueno solo necesitamos un archivo llamado file.txt con los permisos correspondiestes de escritura en caso de duda
Código:
chmod 777 file.txt
En ajax.js he tenido que poner 2 objetos ajax ya que no se puede utilizar uno para mas de una funcion.

Bueno me voy de fiesta que son fiestas!!!!

Se me olvido comentar que con una base de datos seria mas optimo hacerlo pero bueno es que en mi pclinuxos no tengo mysql instalado ;)
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan