使用jQuery和嵌入对象的变量

时间:2014-10-25 06:58:27

标签: php jquery ajax variables

我正在使用一个java脚本库,它允许我从shoutcast服务器查询信息,例如当前播放的歌曲,最近播放的歌曲等等都可以正常工作。该库根据数据的定义ID将数据放入页面中的span元素。

现在,我的问题是我正在尝试将我的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>

对此的任何帮助都会非常有帮助。我看了很多没有运气的地方,这让我非常沮丧。

此致

1 个答案:

答案 0 :(得分:0)

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

这是HTML / JAVASCRIPT

<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!
}


?>

我希望这会帮助那些人,因为我正在寻找一个很好的选择。