This is because PHP is run on the server, and your JS executes on the client. YouJust as an aside, you can do things the other way, so in JS:
<script type="text/javascript">
var myval = '<?php echo $myphpval ?>';
</script>
This is possible because the PHP executes on the server before the JS executes on the client. If however you want to send a JS value to a PHP script, then you will need to do it after the DOM has loaded, usually via an AJAX call.