Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2012, 12:04 PM   PM User | #1
guytrance
New to the CF scene

 
Join Date: Aug 2012
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
guytrance is an unknown quantity at this point
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?
guytrance is offline   Reply With Quote
Old 12-19-2012, 05:22 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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'};
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
guytrance (12-21-2012)
Old 12-19-2012, 07:22 PM   PM User | #3
guytrance
New to the CF scene

 
Join Date: Aug 2012
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
guytrance is an unknown quantity at this point
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
guytrance is offline   Reply With Quote
Old 12-20-2012, 02:34 PM   PM User | #4
hdewantara
Regular Coder

 
hdewantara's Avatar
 
Join Date: Aug 2009
Location: Jakarta, Indonesia.
Posts: 287
Thanks: 4
Thanked 39 Times in 39 Posts
hdewantara is an unknown quantity at this point
Is it simply because
http://www.tranceil.fm/player/readerPlayer.php returns no <track>s ?
hdewantara is offline   Reply With Quote
Users who have thanked hdewantara for this post:
guytrance (12-21-2012)
Old 12-21-2012, 08:17 AM   PM User | #5
hdewantara
Regular Coder

 
hdewantara's Avatar
 
Join Date: Aug 2009
Location: Jakarta, Indonesia.
Posts: 287
Thanks: 4
Thanked 39 Times in 39 Posts
hdewantara is an unknown quantity at this point
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');
hdewantara is offline   Reply With Quote
Users who have thanked hdewantara for this post:
guytrance (12-21-2012)
Old 12-21-2012, 11:13 AM   PM User | #6
guytrance
New to the CF scene

 
Join Date: Aug 2012
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
guytrance is an unknown quantity at this point
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)
guytrance is offline   Reply With Quote
Old 12-21-2012, 11:28 AM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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:
Quote:
&amp; &
&apos; '
&quot; "
&lt; <
&gt; >
__________________
"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

Last edited by AndrewGSW; 12-21-2012 at 11:34 AM..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
guytrance (12-21-2012)
Old 12-21-2012, 11:45 AM   PM User | #8
hdewantara
Regular Coder

 
hdewantara's Avatar
 
Join Date: Aug 2009
Location: Jakarta, Indonesia.
Posts: 287
Thanks: 4
Thanked 39 Times in 39 Posts
hdewantara is an unknown quantity at this point
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 ?
hdewantara is offline   Reply With Quote
Users who have thanked hdewantara for this post:
guytrance (12-21-2012)
Old 12-21-2012, 11:47 AM   PM User | #9
guytrance
New to the CF scene

 
Join Date: Aug 2012
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
guytrance is an unknown quantity at this point
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>
guytrance is offline   Reply With Quote
Old 12-21-2012, 12:08 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
guytrance (12-21-2012)
Old 12-21-2012, 12:31 PM   PM User | #11
guytrance
New to the CF scene

 
Join Date: Aug 2012
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
guytrance is an unknown quantity at this point
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?
guytrance is offline   Reply With Quote
Old 12-21-2012, 02:31 PM   PM User | #12
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"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

Last edited by AndrewGSW; 12-21-2012 at 02:35 PM..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
guytrance (12-21-2012)
Old 12-21-2012, 02:38 PM   PM User | #13
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
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.
__________________
"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

Last edited by AndrewGSW; 12-21-2012 at 02:40 PM..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
guytrance (12-21-2012)
Old 12-21-2012, 05:12 PM   PM User | #14
guytrance
New to the CF scene

 
Join Date: Aug 2012
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
guytrance is an unknown quantity at this point
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)
guytrance is offline   Reply With Quote
Old 12-21-2012, 05:38 PM   PM User | #15
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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 .
__________________
"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

Last edited by AndrewGSW; 12-21-2012 at 06:03 PM..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
guytrance (12-21-2012)
Reply

Bookmarks

Tags
jquery, php, xml

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:18 AM.


Advertisement
Log in to turn off these ads.