英文:
How to set a variable to equal current users username?
问题
如上面的问题所述,我正在尝试将JavaScript中的一个变量设置为用户的用户名值。这是我的代码设置方式:
function submitApproveModal(){
this_file = currentFile;
var approvers = "";
if(document.getElementById('approvers').checked){
approvers = '<?php echo $_SESSION['username']; ?>';
}
approveFileAjax(this_file.uid, approvers);
}
我知道这不是正确的方法,想知道如何重写我的代码,以使approvers等于用户的用户名?
英文:
As the question stated above I am trying to set a variable in my javascript to be the value of the users username. This is how my code is set up:
function submitApproveModal(){
this_file = currentFile;
var approvers = "";
if(document.getElementById('approvers').checked){
approvers = '<?php $_SESSION['username'] ?>';
}
approveFileAjax(this_file.uid, approvers);
}
I know this is not the way for it to work and wondering how exactly can I rewrite my code so that approvers will equal the users username?
答案1
得分: 0
你只是忘记了 "echo":
approvers = '= json_encode($_SESSION['username']) ?>';
但正如之前提到的,混合使用Javascript和PHP是一个不好的主意。
英文:
You simply forgot the "echo":
approvers = '<?= json_encode($_SESSION['username']) ?>';
But as mentioned before, mixing up Javascript and PHP is a bad idea.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论