I'm making a website that uses a lot of different php files. My site uses Wordpress and is a music blog. I have a jPlayer playlist at the top of the screen and for each individual post I want to have a play button to play that song.
I've made a few functions in php and in javascript across a few different php files. How can I declare a global JavaScript variable in one of my php files and access it in another php file? I know that is worded badly, so let me explain.
File 1: my_functions.php (where I set up my jPlayer playlist. The variable song_index is what I want to be global.
<script type = "text/javascript">
var song_index = 0;
var theTitles = new Array();
var theMP3s = new Array();
var jplayer_playlist;
function add_song(title, mp3)
{
theTitles[song_index] = title;
theMP3s[song_index] = mp3;
song_index++;
}
function play_song(index)
{
alert("You want to play song " + index);
//jplayer_playlist.play(index);
}
function get_playlist()
{
var playlist = new Array();
for(var i = 0; i < theTitles.length; i++)
{
playlist[i] = {title: theTitles[i], mp3: theMP3s[i]};
}
return playlist;
}
$(document).ready(function()
{
var playlist = get_playlist();
jplayer_playlist = new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: "#jp_container_1",
oggSupport:false
}, playlist, {
swfPath: "/js",
supplied: "mp3",
wmode: "window"
}
);
});
//]]>
</script>
This sets up my jPlayer playlist dynamically for each page.
File 2 shortcodes.php (this is where I add songs using Wordpress' shortcodes. Here is the code that I use when I want to add a song to the playlist...)
function player_function($args)
{
$url = $args['url'];
$title = $args['title'];
if(!$title)
{
$title = get_the_title();
}
add_song($title, $url);
$ret = "<a href = '" . $url . "'></a><br><a href=\"javascript:void(0)\" onclick = \"play_song(".$_GET['window.song_index'].")\">Play</a>";
return "$ret";
}
Pretty much I just need to access song_index from the first file into a php variable so I can pass it along. As you can see, I tried using $_GET because someone said you can access JS variables this way, but it didn't work. Anyone have any clues?