首页 > 解决方案 > 在 While 循环中将值从一个 PHP 页面传递到另一个而不影响 URL

问题描述

本质上,我有一个用户的个人资料页面,该页面将显示他们与其他用户就他们列出要销售的产品进行的所有活动聊天。然后它会为他们提供一个按钮来访问聊天。我的聊天是如何工作的,它是一个静态页面,其 URL 不应更改,而 chat_id 是包含聊天日志的文件的名称,而 chat_id 是它确定加载哪个文件的方式。问题是我让这个按钮在一个while循环中运行,因此该按钮只采用chat_id的最新值,而不是每个按钮都有自己的chat_id。有什么办法可以帮我解决这个问题吗?TL:DR 在while 循环中的聊天按钮正在使用chat_id 的最新值,希望它使用每个循环的chat_id 值。

包含循环和按钮的 Profile.php 代码

while ($row_all_chat = mysqli_fetch_array($run_all_chat)) {
    $x++;
    $chat_id = $row_all_chat['chat_id'];
    $buyer_id = $row_all_chat['buyer_id'];
    $buyer_name = $row_all_chat['username'];
    $item_name = $row_all_chat['item_name'];
    $item_id = $row_all_chat['item_id'];
    $seller_id = $row_all_chat['seller_id'];
    $_SESSION['x'] = $x;
    
    $_SESSION['chat_id'] = $chat_id;
    $_SESSION['seller_id']= $_SESSION['user_id'];
    $_SESSION['item_id'][$x= $item_id;

    echo
    "
        <div>
            <form method='post' action='chat2.php'>
                <a class='btn btn-primary' href='./chat2.php$chat_id' role='button'>Chat $chat_id with $buyer_name about $item_name X:$x</a>
            </form>
        </div>";
}

}

Chat.php 代码

$chat_id = $_SESSION[$chat_id];
$seller_id = $_SESSION['seller_id'];
$item_id = $_SESSION['item_id'];

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="css/style.css" />
    </head>
    <body>
        <h1>CHAT_ID:<?php echo $chat_id; ?></h1>
        <h1>SELLER_ID:<?php echo $seller_id; ?></h1>

        <div id="wrapper">
            <div id="menu">
                <p class="welcome">Chat<b></b></p>
                <p class="logout"><a id="exit" href="#">Exit Chat</a></p>
            </div>

            <div id="chatbox">
                <?php
                if (file_exists("./chats/$chat_id") && filesize("./chats/$chat_id") > 0) {
                    $contents = file_get_contents("./chats/$chat_id");
                    echo $contents;
                }
                ?>
            </div>

            <form name="message" action="">
                <input name="usermsg" type="text" id="usermsg" />
                <input name="submitmsg" type="submit" id="submitmsg" value="Send" />
            </form>
        </div>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script type="text/javascript">
            // jQuery Document
            $(document).ready(function () {});
        </script>
        <script type="text/javascript">
// jQuery Document
            $(document).ready(function () {
                $("#submitmsg").click(function () {
                    var clientmsg = $("#usermsg").val();
                    $.post("post.php", {text: clientmsg});
                    $("#usermsg").val("");
                    return false;
                });

                function loadLog() {
                    var oldscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height before the request

                    $.ajax({
                        url: "./chats/<?php echo $chat_id?>",
                        cache: false,
                        success: function (html) {
                            $("#chatbox").html(html); //Insert chat log into the #chatbox div

                            //Auto-scroll           
                            var newscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height after the request
                            if (newscrollHeight > oldscrollHeight) {
                                $("#chatbox").animate({scrollTop: newscrollHeight}, 'normal'); //Autoscroll to bottom of div
                            }
                        }
                    });
                }

                setInterval(loadLog, 2500);

                $("#exit").click(function () {
                    var exit = confirm("Return to Item?");
                    if (exit == true) {
                        window.history.back();
                    }
                });
            });
        </script>
    </body>
</html>

标签: phpwhile-loop

解决方案


推荐阅读