1

I have a the code below in my js which obviously works if the js is in the php file

filter: '<?php echo $my_cat>'

how can i make a variable maybe in the php file and pull it in my external js file?

BritishSam
  • 1,070
  • 8
  • 24
  • possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Tchami Feb 20 '15 at 12:46

3 Answers3

1

You have to place this below code at the top part of your test.php page

<script>
var filter = '<?php echo $my_cat>';
</script>

and simply you can check it on your external.js file by alert

alert(filter);
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
0
<?php 
   function showme(){
   echo "HelloWorld";
}
?>    

<script type="text/javascript">
     var my_var = '<?php echo showme(); ?>' ;
</script>
nifCody
  • 2,394
  • 3
  • 34
  • 54
0

You can also use short tags.

<script>

var filter = '<?=$my_cat?>';

</script>
Siter4u
  • 1
  • 1