When your browser requests the file data6_1.js
there is absolutely no php involved in your processing of that request. It is a simple request to a javascript file and that's it. How should the $id2
magically turn into some value it might have inside some other script execution that is not connected at all? You could have that javascript created in a dynamic manner by means of php, but that does not solve your issue at hand, since that would _still be two totally separate script executions that will not share any variables just like that.
Instead you could pass over that value as passive value inside the markup: <input type="hidden" value="<?php echo $id2; ?>">
. Then you can simply select the value in javascript and use it.
In the lower code portion of your question you need to use php to output the value of $id2, otherwise it will be a literal in the html portion of your php script:
<?php
if(!isset($_SESSION['firstname'])) {
$id2 =$_REQUEST['firstname'];
echo "<b>".$id2."</b>"." Logged in";
}
?>
<div id="MNUOBJ7DFA52C14221AD2"
style=" position:absolute; top:549px; left:22px; width:174px; height:34px; z-index:3016;">
<script type="text/javascript" src="data6_1.js"></script>
<script type="text/javascript">
menuItems.([<?php echo $id2; ?>]);
</script>
<a href='scm.php'</a>
</div>
A possible alternative would be to dynamically pack only a single javascript element:
<?php
if(!isset($_SESSION['firstname'])) {
$id2 =$_REQUEST['firstname'];
echo "<b>".$id2."</b>"." Logged in";
}
?>
<div id="MNUOBJ7DFA52C14221AD2"
style=" position:absolute; top:549px; left:22px; width:174px; height:34px; z-index:3016;">
<script type="text/javascript" src="data6_1.js">
var menuItems = [
[
"Collected Material",
"scm.php?firstname=<?php echo $id2; ?>",
"", "", "", "", "0", "0", "", "", "",
],
];
menuItems.([<?php echo $id2; ?>]);
</script>
<a href='scm.php'</a>
</div>
You may be able to shorten <?php echo $id2; ?>
to <?= $id2 ?>
depending on the setup of your php engine. That variant is much more compact and readable.
And don't forget that may have to adjust your "content security policy" header to allow the execution of inline scripts: http://content-security-policy.com/