Ver Mensaje Individual
  #5 (permalink)  
Antiguo 24/09/2007, 07:19
Avatar de AbdelioR
AbdelioR
 
Fecha de Ingreso: septiembre-2006
Ubicación: Tarragona
Mensajes: 926
Antigüedad: 17 años, 7 meses
Puntos: 8
Re: MiniChat con PHP/MySQL/AJAX v2.0

Bueno aquí pongo mi código, que viene a ser el mismo casi:

Principal.html
Código HTML:
<html>
<head>
<title>MiniChat :)</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript"> 
setInterval("fajax3()",1000);
</script>
</head>
<body>
<input type="text" id="comentario" size="50" maxlength="50" />
<input type="button" value="bota" onClick="fajax()" />
<div id="chat">
</div>
<input type="hidden" id="id_hash" value="" />
<script type="text/javascript">
    document.getElementById('comentario').value="";
    document.getElementById('comentario').focus();
 fajax3();
</script>
</body>
</html> 


index.php
Código PHP:
<?php
include("connect.php");
//
if($_GET["Enviar"]=="si")
 {
  
$max="select max(id)+1 from chat2";
  
$max=mysql_query($max);
  
$max=mysql_result($max,0,0);
  
$fecha=date("Y/m/d - H:i:s");
  
//
  
$insert="insert into chat2 values(".$max.",'".$_REQUEST["comentario"]."','".$fecha."')";
  echo 
$insert;
  
$insert=mysql_query($insert);
  exit();
 }
elseif(
$_GET["Leer"]=="si")
 {
  
header("Cache-Control: no-store, no-cache, must-revalidate");
  
$select="select * from chat2 order by id desc limit 0,10";
  
$select=mysql_query($select);
  while(
$row mysql_fetch_array($select))
   {
    if(
$row["comentario"]!=NULL)
     {
        echo 
"<strong>".$row["fecha"]."</strong> - ".$row["comentario"]."<br />";
     }
   }
  exit();
 }
elseif(
$_GET["Hash"]=="si")
 {
  
header("Cache-Control: no-store, no-cache, must-revalidate");
  
$max="select max(id) from chat2";
  
$max=mysql_query($max);
  
$max=mysql_result($max,0,0);
  
//
  
$select="select * from chat2 where id=".$max." limit 1";
  
$select=mysql_query($select);
  
//
  
$id=mysql_result($select,0,"id");
  
$comentario=mysql_result($select,0,"comentario");
  
$fecha=mysql_result($select,0,"fecha");
  
//
  
$hash=$id.$comentario.$fecha;
  if(
$hash==NULL)
   {
    echo 
"vacio";
   }
  else
   {
    
$hash=md5($id.$comentario.$fecha);
    echo 
$hash;
   }
  exit();
 }
?>


ajax.js

Cambiadas ?Enviar=si por index.php?Enviar=si i POST por post
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;
        }
      }
    }
  } 
function ajaxFunction03()
  {
  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 fajax()
{
    var comentario; 
    comentario = document.getElementById('comentario').value;
    var ajax;
    ajax = ajaxFunction01();

    ajax.open("post","index.php?Enviar=si",true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("comentario="+comentario);

    document.getElementById('comentario').value="";
    document.getElementById('comentario').focus();
    fajax3();
}
function fajax2()
 {
    var ajax;
    ajax=ajaxFunction02();
    ajax.onreadystatechange=function()
      {
      if(ajax.readyState==4)
        {
        document.getElementById('chat').innerHTML=ajax.responseText;
        }
      }
    ajax.open("get","index.php?Leer=si",true);
    ajax.send(null);    
 } 
function fajax3()
 {
    var ajax;
    ajax=ajaxFunction03();
    var hashviejo;
    hashviejo=document.getElementById('id_hash').value;
    ajax.onreadystatechange=function()
      {
      if(ajax.readyState==4)
        {
	if(hashviejo!=ajax.responseText && ajax.responseText!='vacio')
	 {
		document.getElementById('id_hash').value=ajax.responseText;
		fajax2();		
	 }        
        }
      }
    ajax.open("get","index.php?Hash=si",true);
    ajax.send(null);
 }
connect.php
Código PHP:
<?php
/*
Esta es la estructura de la tabla
create table chat( id int(5), comentario text, fecha text, primary key(id));
*/
function Conecta()
 {
     
$dbhost 'localhost';  //nombre de host (suele ser 'localhost')
     
$nombreddbb='minombre';
     
$dbuser 'miuser';  //usuario de la base de datos
     
$dbpasswd 'mipass';  //contraseña ...
   
  
if(!($link=mysql_connect($dbhost,$dbuser,$dbpasswd)))
   {
    echo 
"Error al conectar con la base de datos.";
    exit();
   }
  if(!
mysql_select_db($nombreddbb,$link))
   {
    echo 
"Error al elegir la base de datos.";
    exit();
   }
   return 
$link;
 }
$link=Conecta();
?>



A veces el chat me lanza este error cuando refresca:

Warning: Cannot modify header information - headers already sent by (output started at /home/misite/public_html/chat2/connect.php:26) in /home/misite/public_html/chat2/index.php on line 18

Última edición por AbdelioR; 24/09/2007 a las 07:26