[英]How can you safely pass a session variable to javascript?
I have this account balance variable: 我有这个帐户余额变量:
$_SESSION["bitcoin-balance"] = 0;
I need to pass it to a javascript function, which will append the variable in a URL to send to my RESTAPI. 我需要将其传递给javascript函数,该函数会将变量附加在URL中以发送到我的RESTAPI。
function getAccountBalace(username, hash){
//Send to restapi
}
How could I do this? 我该怎么办? Normally, I would just make PHP echo javascript code with the parameters, when the page loads, but I need this function to run when a user clicks a button;
通常,当页面加载时,我只会使PHP带有参数的echo javascript代码,但是当用户单击按钮时,我需要运行此函数; and I cannot have the web page to reload.
而且我无法重新加载网页。
尝试这个
<button onclick="myFunction('<?php echo $_SESSION["bitcoin-balance"]; ?>');" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.