-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

2
  • Please mark the answer, np helping. Commented Nov 23, 2011 at 8:41
  • Isn't this question asked on a daily basis? Commented Nov 23, 2011 at 8:55

4 Answers 4

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.

1
  • +1. The only answer to help the OP help himself. Commented Nov 23, 2011 at 8:55
1
<script>
 var t = "<?php echo $v; ?>";
</script>
1

Try this:

 <script>
  var t = '<?php echo $v; ?>';
 </script>
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.