Ver Mensaje Individual
  #26 (permalink)  
Antiguo 28/09/2007, 02:40
mreyesb
 
Fecha de Ingreso: febrero-2007
Mensajes: 70
Antigüedad: 17 años, 2 meses
Puntos: 1
Re: MiniChat con PHP/MySQL/AJAX v2.0

AVISOS.PHP
Código PHP:
<?php
include("incal/sec.php");
$usuario $_SESSION["s_login"];
$curso $_SESSION["s_curso"]; 
if(
$_GET["Enviar"]=="si")
 {
  
$fecha=date("Y/m/d - H:i:s");
  
$aviso=addslashes(trim(utf8_decode($_REQUEST["comentario"])));
  
//
  
$insert="insert into avisos (nombre_curso,fecha_aviso,aviso,login_rte) values ('$curso','$fecha','$aviso','$usuario')";
  if(
$aviso!=NULL)
   {
    
$insert=mysql_query($insert);
    }
  exit();
 }
elseif(
$_GET["Leer"]=="si")
 {
  
header("Cache-Control: no-store, no-cache, must-revalidate");
  
$select="select * FROM avisos WHERE nombre_curso = '$curso' order by id_aviso desc limit 0,10";
  
$select=mysql_query($select);
  while(
$row mysql_fetch_array($select))
   {
    if(
$row["aviso"]!=NULL)
     {
         echo 
"<strong>".$row["fecha_aviso"]."</strong> - ".$row["aviso"]."<br />";
     }
   }
  exit();
 }
elseif(
$_GET["Hash"]=="si")
 {
  
header("Cache-Control: no-store, no-cache, must-revalidate");
  
$max="select max(id_aviso) from avisos WHERE nombre_curso = '$curso'";
  
$max=mysql_query($max);
  
$max=mysql_result($max,0,0);
  
//
  
$select="select * from avisos where id_aviso = '$max' limit 1";
  
$select=mysql_query($select);
  
//
  
$id=mysql_result($select,0,"id_aviso");
  
$comentario=mysql_result($select,0,"aviso");
  
$fecha=mysql_result($select,0,"fecha_aviso");
  
//
  
$hash=$id.$comentario.$fecha;
  if(
$hash==NULL)
   {
    echo 
"vacio";
   }
  else
   {
    
$hash=md5($id.$comentario.$fecha);
    echo 
$hash;
   }
  exit();
 }
?>
SEC:PHP
Código PHP:
<?php
session_start
(); 
if (
$_SESSION["s_tipo"] != "AL") {   //esto no se ejecuta ya que hago un ingreso 
    
header("Location: ../index.php");     //valido... esto es en caso de que no se
    
exit();                                                 //ingrese login y pass
}
$dbhost='xxxxxxx';
$dbusername='xxxxx';
$dbuserpass='xxxxx';
$dbname='xxxxxx';
mysql_connect ($dbhost$dbusername$dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
?>
HTML:
Código:
<div id="avisoscont">
</div>
		
<input type="text" id="comentario" size="50" maxlength="200" />
<div id="boton1">
<button onClick="fajax()" type="button" class="btnform">Publicar</button>
</div>
<input type="hidden" id="id_hash" value="" />

<script type="text/javascript">
    document.getElementById('comentario').value="";
    document.getElementById('comentario').focus();
 	fajax3();
</script>
AJAX:
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("Su browser no soporta 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("Su browser no soporta 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("Su browser no soporta AJAX!");
        return false;
        }
      }
    }
  }
function fajax()
{
    var comentario; 
    comentario = document.getElementById('comentario').value;
    var ajax;
    ajax = ajaxFunction01();
	ajax.open("POST","avisos.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('avisoscont').innerHTML=ajax.responseText;
        }
      }
    ajax.open("GET","avisos.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","avisos.php?Hash=si",true);
    ajax.send(null);
 }

estructura tabla de la base datos:
id_aviso (primaria int autoincrement)
nombre_curso (foranea)
fecha_aviso
aviso
login_rte

ahora ya no me muestra el error algo moví ya a esta hora no se que.... pero sigue sin mostrar nada.. bueno ahí dejo los codigos haber si me pueden ayudar
saludos

ps: perdon por el doble post

Última edición por mreyesb; 28/09/2007 a las 03:41