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?

1

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

0
<?php 
   function showme(){
   echo "HelloWorld";
}
?>    

<script type="text/javascript">
     var my_var = '<?php echo showme(); ?>' ;
</script>

Comments

0

You can also use short tags.

<script>

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

</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.