I'm trying to set the JavaScript value to a PHP Session variable and get this session value to another php page. here my code. on the same it will show the value using alert. here's my first page index_1.php
<Script>
function(no_user){
`var num_user = no_user;
'<?php $_SESSION["num_user"] = "' + no_user+ '"; ?>';
//alert('<?php echo $_SESSION["num_user"] ?>');*/
window.open("demo.php");
}
</script>
another page index_2.php
<?php
if(isset($_SESSION['num_user'])){
$a = $_SESSION['num_user'];
echo "Number of user: ". $a;
?>
You can not set php variable in js try to set it another location(demo.php)
<script>
function(no_user){
var num_user = no_user;
window.open("demo.php?no_user="+no_user);
//or window.location = 'index2.php?no_user'+no_user;
}
</script>
and set session on demo.php/index2.php
session_start();
if(!empty($_GET['no_user'])) {
$_SESSION['num_user']= $_GET['no_user'];
}
Just make sure to set session_start() in all PHP.Files before accessing $_SESSION-Variables and you're fine. PHP will automagically take care of re-using that session in other .php-files (same server, same domain etc.).