首页 > 解决方案 > 如何存储按钮单击事件后从 PHP 动态传递到 JavaScript 的表数据的值?

问题描述

我一直在尝试在 HTML 表中的 1 对 1 聊天应用程序中列出用户,并在 PHP 中通过名称 ID 动态地唯一定义每个表单元格数据。问题是即使在 PHP 中传递名称值后,我也无法在单击该特定记录的按钮后检索相同的表格单元格值。

<?php

                include("functions.php");

                $token = $user_details[5];
                // Get all Users except the Logged user
                $users_list = get_users_list($user_details[2]);

                ?>
                <table class="table">
                    <thead class="thead-style">
                        <tr>
                            <td class="text-center">Users List</td>
                            <td class="text-center">Login Status</td>
                            <td class="text-center"></td>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            while($fetch = mysqli_fetch_assoc($users_list))
                            {
                        ?>
                        <tr class="table-row-users">
                            <td class="text-center" id="<?php echo $fetch['user_name']; ?>" value="<?php echo $fetch['user_name']; ?>">
                                <?php echo $fetch['user_name']; ?></td>
                            <td class="text-center"><?php echo $fetch['user_login_status']; ?></td>
                            <td class="text-center"><button class="btn btn-outline-primary receiver_email='<?php echo base64_encode($fetch["user_email"]);?>'" id="single_chat" onclick='setReceiver("<?php echo $fetch['user_name']; ?>")'>Chat</button></td>
                        </tr>
                        <?php 
                            }
                        ?>
                    </tbody>
                </table>

JavaScript 代码

function setReceiver(receiver_name)
        {
            var to_user_name = document.getElementById(receiver_name).value;//Unable to Retrieve the Value here
            console.log(to_user_name);
            //$('.receiver').append(to_user_name);
        }

我正面临 JS 方面的错误。这也是我关于 Stack Overflow 的第一个问题。

标签: javascriptphponclickgetelementbyid

解决方案


推荐阅读