Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Call php echo from jQuery

    Hey Guys.

    I have a jQuery player with a stats bar on top that draw echo information from a php file.

    The php file receive info from a remote xspf file (xml).

    The data is echoed good to a simple html page so the data is being transfered.

    But i'm trying to do that using jQuery because the stats bar involves a Scroller and some other things.

    The data just wont output to the stats bar and i am wondering if the code is good.

    The php file:

    PHP Code:
    <?php
    header
    ('Content-Type:text/xml');

    $xml simplexml_load_file("http://**.**.**.**:8000/live.xspf"); 

    echo 
    '<?xml version="1.0" encoding="UTF-8"?><tracks>';
    foreach (
    $xml->trackList->track as $data) {
        
    $radio $data->location;
        
    $song  $data->title;  
        
    $info $data->listeners;
        echo 
    '<track>
                <title>'
    .$song.'</title>
                <listeners>'
    .$info.'</listeners>
                <location>'
    .$radio.'</location>
              </track>'
    ;
    }
    echo 
    '</tracks>';
    ?>
    The jQuery code:

    Code:
    getCurrentTrack();
    
    	$('.now_playing').SetScroller({
    		velocity: 	 50,
    		direction: 	 'horizontal',
    		startfrom: 	 'right',
    		loop:		 'infinite',
    		movetype: 	 'linear',
    		onmouseover: 'pause',
    		onmouseout:  'play',
    		onstartup: 	 'play',
    		cursor: 	 'pointer'
    	});
    	
    	$('.winamp, .popout').click(function(){
    		$("#jquery_jplayer_1").jPlayer("pause");
    	});
    
    function getCurrentTrack(){
    $.get('/player/readerPlayer.php'), function (){
        $track = $(data).find('track');
        $track.each(function (){
            $this = $(this);
            var title = $this.find('title').text();
            var listenersn = $this.find('listeners').text();
            $('.now_playing a').html(title).attr('href',url);
    		$('.listeners span').html(listenersn);
            setTimeout('getCurrentTrack()', 65000);
        });
    }, 'xml'};
    Its then output to the page using:

    Code:
    <div class="now_playing"><div>
    			<a href="#" class="blank">Loading...</a>
    		</div></div>
    Any thoughts?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    What does the error console tell you?

    Anyway ... you are using the variable "data" in the callback of $.get() ... but you don't specify it anywhere so how should jQuery know what it is?

    Second: You should never use a string as first paramter to setTimeout. Please use a function reference or an anonymous function for this
    Code:
    $.get('/player/readerPlayer.php'), function (data){
        $track = $(data).find('track');
        $track.each(function (){
            $this = $(this);
            var title = $this.find('title').text();
            var listenersn = $this.find('listeners').text();
            $('.now_playing a').html(title).attr('href',url);
    		$('.listeners span').html(listenersn);
            setTimeout(getCurrentTrack, 65000);
        });
    }, 'xml'};

  • Users who have thanked devnull69 for this post:

    guytrance (12-21-2012)

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hey buddy.

    Thanx for the reply.

    I Added the data to the function as suggested. but the info still wont show up on the bar and i dont have any errors in the console.

    Here is the link-> Player

  • #4
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    329
    Thanks
    9
    Thanked 49 Times in 48 Posts
    Is it simply because
    http://www.tranceil.fm/player/readerPlayer.php returns no <track>s ?

  • Users who have thanked hdewantara for this post:

    guytrance (12-21-2012)

  • #5
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    329
    Thanks
    9
    Thanked 49 Times in 48 Posts
    And maybe a modified version from devnull69's:
    Code:
    $.get('/player/readerPlayer.php', function (data){
    	$track = $(data).find('track');
    	$track.each(function (){
    		$this = $(this);
    		var title = $this.find('title').text();
    		var listenersn = $this.find('listeners').text();
    		$('.now_playing a').html(title).attr('href',url);
    		$('.listeners span').html(listenersn);
    		setTimeout(getCurrentTrack, 65000);
    	});
    }, 'xml');

  • Users who have thanked hdewantara for this post:

    guytrance (12-21-2012)

  • #6
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hey guys.

    Thanx for your help!

    I Tried all kinds of variations and your suggestions but nothing seems to pull the data off that php file

    Here are some options i tried:

    Code:
    function getCurrentTrack(){
    	$.get('/player/readerPlayer.php', function (data){
    	$track = $(data).find('track');
    	$track.each(function (){
    		$this = $(this);
    		var title = $this.find('title').text();
    		var listenersn = $this.find('listeners').text();
    		$('.now_playing a').html(title).attr('href',url);
    		$('.listeners span').html(listenersn);
    		setTimeout(getCurrentTrack, 65000);
    	});
    }, 'xml')};

    Code:
    function getCurrentTrack(){
    	$.get('/player/readerPlayer.php', function (data){
    	$track = $(data).find('track');
    	$tracks.each(function (){
    		$this = $(this);
    		var title = $this.find('title').text();
    		var listenersn = $this.find('listeners').text();
    		$('.now_playing a').html(title).attr('href',url);
    		$('.listeners span').html(listenersn);
    		setTimeout(getCurrentTrack, 65000);
    	});
    }, 'xml')};
    Code:
    function getCurrentTrack(){
    	$.get('/player/readerPlayer.php', function (data){
    	$tracks = $(data).find('tracks');
    	$tracks.each(function (){
    		$this = $(this);
    		var title = $this.find('title').text();
    		var listenersn = $this.find('listeners').text();
    		$('.now_playing a').html(title).attr('href',url);
    		$('.listeners span').html(listenersn);
    		setTimeout(getCurrentTrack, 65000);
    	});
    }, 'xml')};
    Code:
    function getCurrentTrack(){
    	$.get('/player/readerPlayer.php', function (data){
    	$track = $(data).find('tracks');
    	$tracks.each(function (){
    		$this = $(this);
    		var title = $this.find('title').text();
    		var listenersn = $this.find('listeners').text();
    		$('.now_playing a').html(title).attr('href',url);
    		$('.listeners span').html(listenersn);
    		setTimeout(getCurrentTrack, 65000);
    	});
    }, 'xml')};
    I dont know why i cant see it. the code seems good to go for me.

    (the data off php is outputted if i go to the php page on a browser so its not from their)

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I suspect, according to the XML output:
    Code:
    function getCurrentTrack() {
    	$.get('/player/readerPlayer.php', function (data) {
            $tracks = $(data).find('track');
            $tracks.each(function (){
                $this = $(this);
                var title = $this.find('title').text();
                var listenersn = $this.find('listeners').text();
                $('.now_playing a').html(title).attr('href', url);
                $('.listeners span').html(listenersn);
                setTimeout(getCurrentTrack, 65000);
            });
        }, 'xml');
    };
    Where does url come from..? Presumably you need to extract it from location.
    Code:
    function getCurrentTrack() {
    	$.get('/player/readerPlayer.php', function (data) {
            $tracks = $(data).find('track');
            $tracks.each(function (){
                $this = $(this);
                var title = $this.find('title').text();
                var listenersn = $this.find('listeners').text();
                var locn = $this.find('location').text();
                $('.now_playing a').html(title).attr('href', locn);
                $('.listeners span').html(listenersn);
                setTimeout(getCurrentTrack, 65000);
            });
        }, 'xml');
    };
    But you need to escape the ampersand & and other characters in the XML as it is not valid otherwise:
    &amp; &
    &apos; '
    &quot; "
    &lt; <
    &gt; >
    Last edited by AndrewGSW; 12-21-2012 at 11:34 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    guytrance (12-21-2012)

  • #8
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    329
    Thanks
    9
    Thanked 49 Times in 48 Posts
    Please check again your browsers Error Console then...

    Is there now any messages there either when you run
    http://www.tranceil.fm/player, or
    http://www.tranceil.fm/player/readerPlayer.php ?

  • Users who have thanked hdewantara for this post:

    guytrance (12-21-2012)

  • #9
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hey AndrewGSW

    Your talking about escaping characters from the XML itself on the remote file?

    Cause now it seems i'm getting
    Code:
    "error on line 2 at column 25: xmlParseEntityRef: no name"
    When loading that php on browser.

    These are the first relative lines on the file that generates the xspf:

    Code:
    <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
    <xsl:output omit-xml-declaration="no" media-type="application/xspf+xml"
            method="xml" indent="yes" encoding="UTF-8" />
    <xsl:template match = "/icestats" >
    <playlist version="1" xmlns="http://xspf.org/ns/0/">
    	<title><xsl:value-of select="server" /></title>
    	<creator><xsl:value-of select="server" /></creator>
    	<trackList >
    <!-- end of "header" -->
    
    <xsl:for-each select="source">
    
    <track>
        <location><xsl:value-of select="listenurl" /></location>
    
    
    <xsl:if test="artist"><creator><xsl:value-of select="artist" /></creator></xsl:if>
    <xsl:if test="listeners"><listeners><xsl:value-of select="listeners" /></listeners></xsl:if>
    <title><xsl:value-of select="title" /></title>

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    <?xml version="1.0" encoding="UTF-8"?><tracks><track> 
                <title>Aly &amp; Fila - Aly &amp; Fila - Future Sound Of Egypt 259</title> 
                <listeners>3</listeners> 
                <location>http://94.23.250.14:8000/live</location> 
              </track></tracks>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    guytrance (12-21-2012)

  • #11
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    I suspect, according to the XML output:
    Code:
    function getCurrentTrack() {
    	$.get('/player/readerPlayer.php', function (data) {
            $tracks = $(data).find('track');
            $tracks.each(function (){
                $this = $(this);
                var title = $this.find('title').text();
                var listenersn = $this.find('listeners').text();
                $('.now_playing a').html(title).attr('href', url);
                $('.listeners span').html(listenersn);
                setTimeout(getCurrentTrack, 65000);
            });
        }, 'xml');
    };
    Where does url come from..? Presumably you need to extract it from location.
    Code:
    function getCurrentTrack() {
    	$.get('/player/readerPlayer.php', function (data) {
            $tracks = $(data).find('track');
            $tracks.each(function (){
                $this = $(this);
                var title = $this.find('title').text();
                var listenersn = $this.find('listeners').text();
                var locn = $this.find('location').text();
                $('.now_playing a').html(title).attr('href', locn);
                $('.listeners span').html(listenersn);
                setTimeout(getCurrentTrack, 65000);
            });
        }, 'xml');
    };
    But you need to escape the ampersand & and other characters in the XML as it is not valid otherwise:
    It seems your code is working! though your right...every time the xspf generates the amp sign (&) the php fails to read. i posted the start of the remote xspf file (i need admins approval for posts for some reason and it takes forever so sorry for posting 2 replies)

    the XSPF:

    Code:
    <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
    <xsl:output omit-xml-declaration="no" media-type="application/xspf+xml"
            method="xml" indent="yes" encoding="UTF-8" />
    <xsl:template match = "/icestats" >
    <playlist version="1" xmlns="http://xspf.org/ns/0/">
    	<title><xsl:value-of select="server" /></title>
    	<creator><xsl:value-of select="server" /></creator>
    	<trackList >
    <!-- end of "header" -->
    
    <xsl:for-each select="source">
    
    <track>
        <location><xsl:value-of select="listenurl" /></location>
    
    
    <xsl:if test="artist"><creator><xsl:value-of select="artist" /></creator></xsl:if>
    <xsl:if test="listeners"><listeners><xsl:value-of select="listeners" /></listeners></xsl:if>
    <title><xsl:value-of select="title" /></title>
    How can i escape that? in the xml it self? or in php?

  • #12
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If you are using XSLT 2.0+ then you could use encode():

    Code:
    <title><xsl:value-of select="url:encode(title)" /></title>
    although it is preferable that the source-XML is corrected.
    Last edited by AndrewGSW; 12-21-2012 at 02:35 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    guytrance (12-21-2012)

  • #13
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    or in php?
    I don't know if this can be done in PHP; probably, but not simply. You would probably need to read the file as a text file, replace the & and other characters, re-store this new file on the server, then re-parse it as XML.

    But the original XML file is malformed and should be corrected.
    Last edited by AndrewGSW; 12-21-2012 at 02:40 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    guytrance (12-21-2012)

  • #14
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    But the original XML file is malformed and should be corrected.
    I would go for that yes.

    But the question is, how? hehe how do i go about fixing these issues..i have very limited understanding of XML parsing, so what should i look for if the data is auto generated by whats actually playing on the server?

    btw the xspf i attached above is the actual XML file in the production server. (xspf.xml)

  • #15
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    My experience of XML and related technologies is limited, but cannot you or someone else modify the xspf file to include the encode() function I suggested?

    Added: I'm guessing it's like this:

    the php is requested;
    this php page loads and executes the xspf file, which is an XML-Transform file - which transforms an XML document from one XML-structure to another;
    the transform get's its source data from either an XML-document or a database - I suspect, a database;
    this all executes on the server before returning the (final) XML data.

    So there is no source to modify, unless the database-data itself can be modified. So, as I see it, modifying the xspf.xml is the solution.

    But I may be completely wrong .
    Last edited by AndrewGSW; 12-21-2012 at 06:03 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    guytrance (12-21-2012)


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •