Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-23-2006, 05:42 PM   #1 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Read web page into an array

I've written a php script to read my current winamp playlist. I need to write javascript to call upon http://josh.avclan.net/playlist.php and read the output into an array of song titles. I don't have a problem doing it in php, but I don't do much javascript, so if anyone can help me out, that'd be fantastic.

I plan to throw this bit of javascript onto my myspace page, otherwise I'd just stick with php

Thanks
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-23-2006, 07:14 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
i have json installed on the server. you can convert a php to a javascript array string by using this:
PHP Code:
<?
$array 
= array('apple''orange''pear');
echo 
json_encode($array);
?>
then u can use some ajax stuff like this:
HTML Code:
<script>
// start ajax stuff
var xmlHttp;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function startRequest(method, URL) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open(method, URL, true);
	xmlHttp.send(null);
}

function handleStateChange() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			processResponse(xmlHttp.responseText);
		}
	}
}

function processResponse(response) {
	my_songs = eval(response);

}
// end ajax stuff

// initialize your song array
var my_songs;

// make ajax request
startRequest('GET', 'my_php_script.php');

// now my_songs is set to your php array
</script>
sde is offline   Reply With Quote
Old 03-23-2006, 08:57 PM   #3 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Thanks Mike, I'm really close to getting this working. The only problem is that my_songs is still "undefined", it isn't getting set to anything, and ajax is greek to me. Arg...

You can see the php version of it at http://josh.avclan.net/ though
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-24-2006, 08:42 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
i can't test it myself right now, but try moving the 'var my songs' line to the top.
sde is offline   Reply With Quote
Old 03-24-2006, 09:25 AM   #5 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Yeah I thought of that, didn't change anything. Am I correct in pointing the startRequest() function to http://josh.avclan.net/playlist.php ?

The xmlHttp.readyState never changes to 4, it is still 1 when the page loads. I'm trying to look up information on ajax to figure it out though
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-24-2006, 10:10 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
change the processResponse to this to make sure it is being triggered and you are getting output:
HTML Code:
function processResponse(response) {
  alert(response);
  my_songs = eval(response);

}
another thing you may want to try is initialize the song var to an empty array
HTML Code:
var my_songs = new Array();
sde is offline   Reply With Quote
Old 03-24-2006, 10:19 AM   #7 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
processResponse isn't getting triggered because xmlHttp.readyState = 1 and doesn't seem to change. I stripped out the if statements in handleStateChange(), but obviously if the readyState isn't changing from 1, there's no data to eval()

I'm assuming a readyState of 1 means the code isn't able to render the page? Any idea why that would be? It wouldn't have to do with security or permissions would it?
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-24-2006, 10:49 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
are you running the page locally? there could be a browser security issue. make sure the files are uploaded to the server. i just tested this code and it works.

test.html
HTML Code:
["First Song","Second Song"]
mypage.html
HTML Code:
<script language="javascript">
var my_songs = new Array();

// start ajax stuff
var xmlHttp;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function startRequest(method, URL) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open(method, URL, true);
	xmlHttp.send(null);
}

function handleStateChange() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			processResponse(xmlHttp.responseText);
		}
	}
}

function processResponse(response) {
	my_songs = eval(response);
	
	for( x in my_songs ) {
		alert( my_songs[x] );
	}
}

// make ajax request
startRequest('GET', 'test.html');
</script>
sde is offline   Reply With Quote
Old 03-24-2006, 11:07 AM   #9 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Awesome, it's working now. Not sure what I was doing wrong before, though I might have been trying to access the my_songs array outside of the scope of processResponse. Maybe if I had made it global, I wouldn't have had any issues?

Either way, it works great now. Thanks again Mike
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-24-2006, 11:36 AM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
now that you have a little AJAX structure setup, you can get creative with your song list.

for example, you can use a javascript timeout function to re-call startRequest every 20 seconds or so. Then your songlist could refresh without the browser refreshing.

i'm not sure how much other information winamp provides, but you could probably do some cool stuff.
sde is offline   Reply With Quote
Old 03-24-2006, 11:41 AM   #11 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
I'm interfacting with a pre-existing plugin which turns winamp into a web server. It can accept requests to control playback, change playlists, turn up the volume etc. Basically the plugin allows you to turn winamp into a remote-controlled media player.

If I can figure it out, I'll add play controls to the script, and maybe even configure it to stream audio.
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-24-2006, 11:56 AM   #12 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Dammit. Myspace doesn't support javascript. Go figure. Let's see if I can find a language that myspace will allow.
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-24-2006, 12:47 PM   #13 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Why not make your own blog elsewhere, then all your myspace friends will be jealous.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-24-2006, 12:55 PM   #14 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Well, that's actually what I'm working on and the reason why I wrote the php side of it in the first place. My blog is a work in progress and it's placeholder is here: http://josh.avclan.net

I just wanted to get it working on myspace to see if I could do it, and I'd probably be the first person to have something like that on his myspace...but for security reasons they can't allow javascript support on myspace. Bastards.
__________________
Current Project
Redline is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Parameters to web page mohanapriya MS Technologies ( ASP, VB, C#, .NET ) 0 04-18-2005 04:01 AM
how do i read a web page into a string? sde Java 1 01-13-2005 06:45 AM
reading a web form for input variables sde PHP 3 08-12-2003 10:02 AM
web page icons Cyyb HTML, XML, Javascript, AJAX 10 01-11-2003 04:05 PM


All times are GMT -8. The time now is 05:43 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting