我试图将查询结果打印到我的proyect的其他文件中调用的div中。我有结果的文件被称为chat.php
并从index.php
调用如何将结果打印到div chat
这是来自chat.php的代码
<?php session_start();
include 'db.php';
require_once '../functions.php';
comprobarSession();
$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;
?>
<?php
//Una función para mostrar los datos
function imprimir($arrDatos)
{
if ($arrDatos)
{
echo "<hr />SE ENCONTRARON ".count($arrDatos). " REGISTROS<br /><hr />";
/**
* Construímos los datos de forma limpia
*/
$strHtml='CHAT:<br />';
foreach ($arrDatos as $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;
}
}
?>
和index.php中的这个
<?php session_start();
include 'db.php';
include '../functions.php';
$emit = obtener_mensajes($conexion, $us);
comprobarSession();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="style.css">
<script>
function ajax(){
var req = new XMLHttpRequest();
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>
<body onload="ajax();">
<div id="container">
<div id="chat_box">
<div id="chat">HERE IS WHERE CHAT.PHP NEED TO BE</div>
</div>
<form action="index.php" method="POST">
<textarea name="message" placeholder="Enter message"></textarea>
<input type="hidden" name="nombre" placeholder="Name" value="<?php echo $_SESSION['usuario']['nombre']?>">
<input type="submit" name="submit" value="Send it">
<?php foreach ($emit as $msg): ?>
<input type="hidden" id="myId" name="idReceiver" value="<?php echo $msg['idEmitter']; ?>">
<input type="hidden" name="idEmitter" value="<?php echo $us ?>">
<?php endforeach ?>
</form>
<?php
if (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>
版
函数是主文件夹
中的文件答案 0 :(得分:0)
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 datos
function imprimir($arrDatos)
{
if ($arrDatos)
{
echo "<hr>SE ENCONTRARON ".count($arrDatos). " REGISTROS<br><hr>";
/**
* Construímos los datos de forma limpia
*/
$strHtml='CHAT:<br>';
foreach ($arrDatos as $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;
}
}
?>
index.php的内容
<?php session_start();
include 'db.php';
include '../functions.php';
$emit = obtener_mensajes($conexion, $us);
comprobarSession();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="style.css">
<script>
function ajax(){
var req = new XMLHttpRequest();
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>
<body onload="ajax();">
<div id="container">
<div id="chat_box">
<div id="chat"><?php include("chat.php");?></div>
</div>
<form action="index.php" method="POST">
<textarea name="message" placeholder="Enter message"></textarea>
<input type="hidden" name="nombre" placeholder="Name" value="
<?php echo $_SESSION['usuario']['nombre']?>">
<input type="submit" name="submit" value="Send it">
<?php foreach ($emit as $msg): ?>
<input type="hidden" id="myId" name="idReceiver" value="
<?php echo $msg['idEmitter']; ?>">
<input type="hidden" name="idEmitter" value="<?php echo $us ?>">
<?php endforeach ?>
</form>
<?php
if (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>
考虑到chat.php和index.php在同一个文件夹中,并且到db.php和functions.php的路由都没问题,这应该可以正常工作。