将PHP变量传递给AJAX请求

时间:2018-06-27 08:40:00

标签: javascript php ajax

如何将php变量的值传递给AJAX请求?我在这里使用会话,从另一个php文件(header.php)开始。 变量为“ $ _SESSION ['name']”,我想将其作为参数而不是“ XXX”传递(该功能由按钮调用)。

<?php include("header.php"); ?>
<script>
function sendFunc()
            {
                var message = document.getElementById('msg_text').value;
                new Ajax.Request("chat_room_insert.php",
                    {
                        method: "post",
                        parameters: {id: 0, name: "XXX", msg_text: message},
                        onSuccess: function() {},
                        onFailure: function() { alert("failure"); },
                        onException: function() { alert("exception"); }
                    });
            }
        </script>

1 个答案:

答案 0 :(得分:-1)

更改

parameters: {id: 0, name: "XXX", msg_text: message},

parameters: {id: 0, name: "<?= $_SESSION['name'] ?>", msg_text: message},
相关问题