0

I figured out sometimes assigning php variable to javascript is fine but assigning javascript variable value to php's variable is not usually assigned

Option 1 :

  <script type="text/javascript">

  <?php 
  if($page == "home"){ 
   echo 'var page = "home"';
  }else{ ?>
   echo 'var page = "non-home"';
  } ?>

  </script>

Option 2:

<script type="text/javascript">

var page = "home";

if(page == "home"){
<?php $page = "home"; ?>    
}else{
<?php $page = "non-home"; ?>   
}

</script>

In the above 2 option, Is it option 2's php variable "$page" will not be assigned because the server will run php code first?

Can I conclude that I can assign any php variable to javascript variable but cannot assign javascript variable to php variable?

codec
  • 47
  • 1
  • 7
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Qirel Jun 23 '18 at 08:42
  • 1
    You can't set a PHP variable through JavaScript, only the other way around. PHP executes and finishes before JavaScript starts its execution (serverside (PHP) vs clientside (JS), and serverside is executed and finished before clientside). – Qirel Jun 23 '18 at 08:43

0 Answers0