如何将变量设置为等于当前用户的用户名?

huangapple go评论72阅读模式
英文:

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 = &quot;&quot;;
  if(document.getElementById(&#39;approvers&#39;).checked){
    approvers = &#39;&lt;?php $_SESSION[&#39;username&#39;] ?&gt;&#39;;
  }
  
  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 = '';

但正如之前提到的,混合使用Javascript和PHP是一个不好的主意。

英文:

You simply forgot the "echo":

approvers = &#39;&lt;?= json_encode($_SESSION[&#39;username&#39;]) ?&gt;&#39;;

But as mentioned before, mixing up Javascript and PHP is a bad idea.

huangapple
  • 本文由 发表于 2023年6月8日 04:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定