dongtan9465 2013-02-14 22:38
浏览 44
已采纳

如何在PHP中使用JavaScript变量? [关闭]

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?

  • 写回答

2条回答 默认 最新

  • dsuvs66406 2013-02-14 22:45
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)