search

Home  >  Q&A  >  body text

How do I set a variable equal to the current user's username?

As mentioned in the question above, I'm trying to set a variable in my javascript as the value of the user's 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 how it works and was wondering how I could rewrite my code so that the approver equals the user's username?

P粉914731066P粉914731066302 days ago362

reply all(1)I'll reply

  • P粉996763314

    P粉9967633142024-01-17 16:28:07

    You just forgot about "echo":

    approvers = '<?= json_encode($_SESSION['username']) ?>';

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

    reply
    0
  • Cancelreply