将 PHP 变量传递给 Javascript 函数
我试图将 PHP 变量传递给 Javascript 函数,但它一直显示为“未定义”。我将 Javascript 放在单独的页面上,并将其包含在我调用该函数的页面上。这是代码:
<?php $repname = $_SESSION['REPNAME']; ?>
<script type="text/javascript" src="js/chat.js"></script>
<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText('<?php echo $repname;?>');" />
这是 js/chat.js:
function sendChatText(repname) {
alert(repname);
}
I am trying to pass a PHP variable to a Javascript function but it keeps displaying as 'undefined'. I have the Javascript on a separate page and am including it on the page that I am calling the function from. Here is the code:
<?php $repname = $_SESSION['REPNAME']; ?>
<script type="text/javascript" src="js/chat.js"></script>
<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText('<?php echo $repname;?>');" />
Here is js/chat.js:
function sendChatText(repname) {
alert(repname);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在设置 $repid 但回显 $repname
You are setting $repid but echoing $repname
我仍然不知道为什么这不起作用,但我通过将该 Javascript 函数添加到 PHP 页面并执行以下操作来解决它:
I still don't know why this isn't working but I got around it by adding that Javascript function to the PHP page and doing this: