Printresults php within a div

0

I'm trying to print the result of a query inside a div that is in another file, the first file chat-php is called by the GET method to the file index.php that is in the same folder

this is the file code 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;
    }
}
?>

and this is the 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"></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>

at the moment I want to print everything appears blank  

I hope someone can help me

    
asked by Cesar Gutierrez Davalos 24.07.2017 в 20:54
source

0 answers