将变量与jQuery和嵌入对象一起使用


Using variables with jQuery and embedded objects

我使用的是一个java脚本库,它允许我从shoutcast服务器查询信息,如当前播放的歌曲、最近播放的歌曲等,这些都很好。这个库根据其定义的ID将数据放入页面上的span元素中。

现在,我的问题是,我试图将我的跨度的内容(一个字符串(当前歌曲标题))传递给PHP,以便我可以将其用于我的Twitter库,该库使用PHP发布到Twitter。

<?php
// Insert your keys/tokens
$consumerKey = '';
$consumerSecret = '';
$accessToken = '';
$accessTokenSecret = '';
// Full path to twitterOAuth.php (change OAuth to your own path)
require_once('/home/soundcheck/public_html/app/twitter/auto/twitteroauth.php');
require_once('/home/soundcheck/public_html/app/twitter/auto/twitter.class');
// create new instance
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$twitter->send('testing...');       // This will send testing to twitter status!
?>
<html>
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="jquery.shoutcast.min.js"></script>
    <!-- Current Song Played -->
    <script>
    // Get current song playing and load it into an element with an ID of songtitle
        $.SHOUTcast({
            host : 'live.soundcheck.xyz',
            port : 8000,
            interval : 5000,
        }).stats(function(){
            $('#songtitle').text(this.get('songtitle'));
            $(document).ready(function() {
                console.log("Document Ready!");
                var content = $('#songtitle').text();
                var nowplaying = ("#NowPlaying: " + content);

                $.ajax({
                    url: 'receiver.php',
                    type: 'POST',
                    data: { data : nowplaying },
                    success: function (result) {
                        console.log(nowplaying);
                    }
                });
            });
        });
    </script>
    <!-- Last 10 Songs Played -->
    <script>
    // Get last 10 songs playing and load it into an ul element
        $.SHOUTcast({
            host : 'live.soundcheck.xyz',
            port : 8000
        }).played(function(tracks){
            $('ul').html('');
            $.each(tracks,function(k,track){
                $('ul').append('<li>'+track.title+'</li>');
            });
        });
    </script>
</head>
<body>
This SPAN has the current song title within it upon page load which is good. I want to pass this     data to my PHP above to post to twitter.
    <span id="songtitle" name="songtitle"></span>
    <ul></ul>
</body>
</html>

这方面的任何帮助都将非常有帮助。。我看了很多地方都不走运,这让我非常沮丧。

问候,

我发布了我的更新代码,它在页面加载时使用AJAX成功地将jQuery变量发送到PHP。

这是HTML/JJAVASCRIPT

<html>
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="jquery.shoutcast.min.js"></script>
    <!-- Current Song Played -->
    <script>
        // Get current song playing and load it into an element with an ID of songtitle
        $.SHOUTcast({
            host : 'live.soundcheck.xyz',
            port : 8000,
            interval : 5000,
        }).stats(function(){
            $('#songtitle').text(this.get('songtitle'));
            $(document).ready(function() {
                console.log("Document Ready!");
                var content = $('#songtitle').text();
                var nowplaying = ("#NowPlaying: " + content);
                console.log('TOP' + nowplaying);
                $.post("receiver.php", //Required URL of the page on server
                    {   // Data Sending With Request To Server
                        name:nowplaying,
                    },
                    function(response){  // Required Callback Function
                        alert("Response: " + response);  //  "response"  receives - whatever written in echo of above PHP script.
                });
            });
        });
    </script>
    <!-- Last 10 Songs Played -->
    <script>
        // Get last 10 songs playing and load it into an ul element
        $.SHOUTcast({
            host : 'live.soundcheck.xyz',
            port : 8000
        }).played(function(tracks){
            $('ul').html('');
            $.each(tracks,function(k,track){
                $('ul').append('<li>'+track.title+'</li>');
            });
        });
    </script>
</head>
<body>
    <span id="songtitle" name="songtitle"></span>
    <ul></ul>
</body>
</html>

以下是PHP代码

<?php
// Insert your keys/tokens
$consumerKey = '';
$consumerSecret = '';
$accessToken = '';
$accessTokenSecret = '';
// Full path to twitterOAuth.php (change OAuth to your own path)
require_once('/home/soundcheck/public_html/app/twitter/auto/twitteroauth.php');
require_once('/home/soundcheck/public_html/app/twitter/auto/twitter.class');
// create new instance
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
if($_POST["name"])
{
    $name = $_POST["name"];
    // Here, you can also perform some database query operations with above values.
    // echo "Welcome ". $name ."!"; // Success Message
    echo $name; // Success Message
    $tag = ('@ http://soundcheck.xyz  #radio - powered by: http://buzzzhost.com');
    $twitter->send($name .= $tag);       // This will send testing to twitter status!
}

?>

我希望这能帮助到其他人,因为我一直在寻找一个好的选择。