Skip to content Skip to sidebar Skip to footer

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 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;
    }
}
?>

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>
    <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>

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"