-1

I.ve this thing in php

$v = "test";

How do i pass this to javascript

I did like this

 <script>
  var t = <?php echo $v; ?>
 </script>

I'm passing that t to URL But ii says undefined

4 Answers4

2

If you look at the source of the page in your browser (with your first attempt), you see:

<script>
 var t = test
</script>

And from here, you could guess what's amiss.

Rodolphe
  • 1,689
  • 1
  • 15
  • 32
1
<script>
 var t = "<?php echo $v; ?>";
</script>
1

Try this:

 <script>
  var t = '<?php echo $v; ?>';
 </script>
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
0
<?php
    echo "<script type='text/javascript'>var t = '$v';</script>";
?>

Javascript runs clientside and php runs server side so you can't just pass one straight through to the other - they don't run at the same time. You can use php to create javascript to run later though.

Helen
  • 811
  • 5
  • 15