Print Result Over A Div
I'm trying to print the result of my query into a div that is called in other file of my proyect. thefile where i have the resulta is called chat.php and is called from index.php h
Solution 1:
Content of chat.php
<?php$id=$_GET['id'];
//var_dump($id)?><?php$sql = "SELECT ue.nombre de, ur.nombre a, c.message FROM messages c
INNER JOIN usuarios ue ON c.idEmitter = ue.idUsuario
INNER JOIN usuarios ur ON c.idReceiver = ur.idUsuario
WHERE (c.idEmitter = :usr1 AND c.idReceiver = :usr2)
OR (c.idEmitter = :usr2 AND c.idReceiver = :usr1)
ORDER BY sent ASC";
$usr1=$id;
$usr2=$us;
$stmt = $conexion->prepare($sql);
$stmt->bindParam("usr1",$usr1);
$stmt->bindParam("usr2",$usr2);
$stmt ->execute();
$arrDatos = $stmt->fetchAll(PDO::FETCH_ASSOC);
//var_dump($arrDatos);
imprimir ($arrDatos);
$pdo = null;
//Una función para mostrar los datosfunctionimprimir($arrDatos)
{
if ($arrDatos)
{
echo"<hr>SE ENCONTRARON ".count($arrDatos). " REGISTROS<br><hr>";
/**
* Construímos los datos de forma limpia
*/$strHtml='CHAT:<br>';
foreach ($arrDatosas$row)
{
//'<div id="chat_data">'$strHtml.='<span style="color: green;>'.$row["de"].':
</span>'.$row["message"].'<br />';
$strHtml.='<span style="color: green;>'.$row["a"].':
</span>'.$row["message"].'<br />';
//'</div>'
}
echo$strHtml;
}
}
?>
Content of index.php
<?php session_start();
include'db.php';
include'../functions.php';
$emit = obtener_mensajes($conexion, $us);
comprobarSession();
?><!DOCTYPE html><html><head><title></title><linkrel="stylesheet"href="style.css"><script>functionajax(){
var req = newXMLHttpRequest();
req.onreadystatechange = function(){
if (req.readyState == 4 && req.status == 200) {
document.getElementById('chat')
.innerHTML =req.responseText;
}
}
var myId = document.getElementById('myId');
var url = 'chat.php?id='+myId;
req.open('POST', url, true);
req.send();
}
setInterval(function(){
ajax()
}, 1000);
</script></head><bodyonload="ajax();"><divid="container"><divid="chat_box"><divid="chat"><?phpinclude("chat.php");?></div></div><formaction="index.php"method="POST"><textareaname="message"placeholder="Enter message"></textarea><inputtype="hidden"name="nombre"placeholder="Name"value="
<?phpecho$_SESSION['usuario']['nombre']?>"><inputtype="submit"name="submit"value="Send it"><?phpforeach ($emitas$msg): ?><inputtype="hidden"id="myId"name="idReceiver"value="
<?phpecho$msg['idEmitter']; ?>"><inputtype="hidden"name="idEmitter"value="<?phpecho$us?>"><?phpendforeach?></form><?phpif (isset($_POST['submit'])) {
$name = $_POST['nombre'];
$message = $_POST['message'];
$emitter = $_POST['idEmitter'];
$receiver = $_POST['idReceiver'];
$query = "INSERT INTO messages (nombre, message, idEmitter,
idReceiver, seenUsuario) VALUES ('$name', '$message', '$emitter',
'$receiver', '0')";
$run = $conexion->query($query);
}
?></div></body></html>
This should work as intended considering that chat.php and index.php are on the same folder and the routes to db.php and functions.php are fine.
Post a Comment for "Print Result Over A Div"