Hi, I need to pass some PHP values to Javascript. I tried the following example: <? $vartopass = "Hello"; ?> <script = "javascript"> var strnewVar = '<?=$vartopass?>'; alert(strnewVar); </script> But the screen output is "<?=$vartopass?>" instead of "Hello". What's wrong with my code? How to make it work? Thanks a lot for any help.
Perhaps you don't have PHP short tags on (I'm assuming this is in a PHP script.) Try: <?php echo $vartopass; ?>
Thank you Danltn for the reply. I tried your code, but the alert didn't show at all this time. Do you know what the reason probably is? Thank you very much.
<script = "javascript"> var strnewVar = '<?=$vartopass?>'; alert(strnewVar); </script> Should be... <script type="text/javascript"> ... </script>