I have a HTML page with a script tag with PHP in it as follows:
<div id="myName">
<script type="text/javascript">
var myName = <?php
if(isset($_SESSION['name']) && $_SESSION['name'] != ''){
echo $_SESSION['name'];
}
else
echo "NotSet";
?>;
document.write(myName);
</script>
</div>
But running this actually prints like below:
<div id="myName">
var myName=SomeName;
document.write(myName);
</div>
Expected:
<div id="myName">
SomeName
</div>
On doing inspect element:
<div id="myName">
<script type="text/javascript">
"var myName=SomeName;
document.write(myName);"
</script>
</div>
I want to post-process the variable myName and derive other values ahead in JS logic. So, I need to get the php value in JS variable.
Please note: I am not asking how to get a PHP variable into JS variable (for which there are many different questions on Stack Overflow). This is a different question and hence not a duplicate for those questions.