Ver Mensaje Individual
  #2 (permalink)  
Antiguo 04/07/2011, 10:28
Avatar de Melecio
Melecio
 
Fecha de Ingreso: julio-2011
Ubicación: Coahuila
Mensajes: 320
Antigüedad: 12 años, 10 meses
Puntos: 8
Respuesta: Ayuda con mi chat con mysql y ajax . La bd no se actualiza

pues hay tienes le cambie el codigo para mostrar y enviar datos del chat

use ajax para esto pruebalo aber que tal






index.html
-----------------------------
<html>
<head><title>Chat Logeo</title></head>
<body>
<form action = "chat.php" method = "POST">
Usuario : <input type = "text" name = "username">
<input type = "submit" value = "Login">
</form>
</body>
</html>


add.php
------------------------------------------------------------
<?php
session_start();
$conn = mysql_connect("localhost","root","");
mysql_select_db("chatbd",$conn);
$msj = $_GET["message"];
$user = $_SESSION["user"];
echo $user . " dice: " . $msj;
$sql = "insert into messages(user, message) values('$user', '$msj')";


mysql_query($sql, $conn);
?>



mensajes.php
--------------------------------------------------------------
<?php

$conn = mysql_connect("localhost","root","");
mysql_select_db("chatbd",$conn);

$rst = mysql_query("select * from messages", $conn);





$num = mysql_num_rows($rst);

$salida = "";

if($num > 0)
{
for($i = 0 ; $i < $num; $i++)
{
$salida .= mysql_result($rst, $i, 1) . " dice : " . mysql_result($rst, $i, 2) . "<br/>";

}


}


echo $salida;



?>


chat.php
--------------------------------------------
<?php
session_start();
session_register("user");


$_SESSION['user'] = $_POST['username'];

$user = $_SESSION['user'];
?>


<html>
<head><title><?php echo $user; ?> - Chateando</title>
<script type="text/javascript">

function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}

var http = getXMLHTTPRequest();

function getstatussubestaciones() {
var myurl = 'mensajes.php';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}


function useHttpResponse() {
if (http.readyState == 4) {

if(http.status == 200) {


var mensaje = http.responseText;



document.getElementById('chat').innerHTML = mensaje;




setTimeout('getstatussubestaciones()', 1000);

}
} else {

}
}
</script>

<script type="text/javascript">
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}

var http = getXMLHTTPRequest();

function proceso() {

name = encodeURIComponent(document.getElementById("messag e").value);
// ejecuta la página inicio.php desde el servidor
http.open("POST", "add.php?message=" + name, true);
// define la función para manejar las respuestas del servidor

// haz la petición al servidor
http.send(null);
getstatussubestaciones();


}

</script>



<script type="text/javascript">
function pulsar(e) {
tecla = (document.all) ? e.keyCode : e.which;
if (tecla==13) {
proceso();
}
}
</script>


<link href="c.css" rel="stylesheet" type="text/css">
</head>

<body onLoad="getstatussubestaciones()">

<div id = "chat" style = "height: 200px; overflow:auto;">
</div>

<div align="center"></div>
<label>
<input type="text" name="message" id="message" onkeypress = "pulsar(event)" >
</label>
<label>
<input type="button" name="button" id="button" value="Bot&oacute;n" onClick="proceso()">
</label>
<p align="center">&nbsp;</p>
</body>
</html>