dongxian3852 2017-07-24 19:55
浏览 47

在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 how can i print the result into divchat

this is my code from 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 one 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">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>

Edition

this is what print when i chang the code enter image description here

functions is a file that are in the principal folder

enter image description here

  • 写回答

1条回答 默认 最新

  • douren6874 2017-07-24 20:41
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 用lstm来预测股票价格
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上