Javascript and PHP don’t actually know anything about one another. However, you can use PHP to write to JavaScript. So in my_functions.php you could do this:
<?php
$myGlobalSongIndex = '0'; // or however you want to assign this else....
?>
<script type = "text/javascript">
var song_index = <?php print myGlobalSongIndex; ?>;
Then in shortcodes.php on the write link line, print $myGlobalSongIndex
instead of the $_GET['window.song_index']
that you are doing now. The one thing that you need to make sure of is that $myGlobalSongIndex
is in scope in both places. Meaning if it is inside a function or class, you will need to use the global
keyword.
There is a downside to this exact approach. You will not be able to separate the javascript into a separate file which can be quite nice in many case. You could write just the following in the php, as part of the html’s head
section:
<?php
$myGlobalSongIndex = '0'; // or however you want to assign this else....
?>
<script type = "text/javascript">
var song_index = <?php print myGlobalSongIndex; ?>;
</script>
And make sure that you load the external javascript file afterwards. Then when you use the song_index
in the javascript file, you would probably want to make sure it is indeed initialized for any pages where you may include it without rendering the field in php.
1
solved How do I use a JavaScript variable in PHP? [closed]