Hello
I am after something and I hope it can be done, I don’t know much about JavaScript.
I have a list of music on my website i want this to happen when the viewer clicks one of the songs a popup contact window opens where they put there name and where there from and in the comment box is the request they clicked.
and as i update my music a lot it would be good if I could read a XML file
if you don’t understand what I am getting at then let me know.
So far as the XML file is concerned, server-side code such as PHP or ASP is better suited to read in the data and create the HTML content from it. Why is server-side code better for that? Because it will continue to work even when the client-side scripting is turned off or disabled.
I don’t understand what you mean by the “request they clicked”.
Do you want the text name of the song to appear in the comments, do you want them to be able to purchase the song from you, or do you want a player so that they can immendiately play the song?
Some examples from other similar web pages might help us to understand better what you want to achieve.
Okay, to summarise - javascript scripting is best used to enhance the user experience in some way. If it’s used as a mandatory part of your site, then you will find that around 5% of your visitors don’t have it enabled (by office IT policy) or they have deliberately turned it off as a security precaution.
What that means is, if you use javscript to provide the content (songs from the xml file) then around 5% of your visitors won’t be able to see or do anything.
Email is not done by javascript. That’s something that server-side code such as PHP or ASP handles instead.
What happens is that the web page submits a form to the server. The server receives the form, retrieves the values from that form and sends those values as an email to yourself.
It can be possible to do it without involving a server-side code, where you submit the form to “mailto:me@somewhere.com”, but you’re then playing a percentage game. The mailto: protocol tries to open up the persons local email program. Many people (25% or more now) do not have a local email program. If they use hotmail, or yahoo, or gmail, or any other web-based email system, you commonly will find that they do not have an email program with which to send you email. You commonly cannot tell the web browser to send emails via places like yahoo. Instead, the person will just get an error from their computer.
Your best path to success in this case, is for server-side coding to occur. Anything else and you make it more and more likely that your page will fail for the visitor.
the email would not come from the mailto:me@somewhere.com as i want a pop up contact box i have had a quick look something like this …/Radio_PHP/playlist_mix.php?letter=a seeing by the url thats done in php
Perhaps then you would like to get in touch with the PHP forum about this?
The alert box part can be easily performed as a scripting action, from an existing on-page form, after the more fundamental parts of the system are in place.
If you wanted to use XML as the datasource, you could simple change the getTracklistings function.
Assuming you have an XML file like this:-
<tracks>
<track>
<id>
1
</id>
<name>
Rick Astley - Never Gonna Give You Up
</name>
</track>
<track>
<id>
2
</id>
<name>
ODB - Shimmy Shimmy Ya
</name>
</track>
<track>
<id>
3
</id>
<name>
Jimi Hendrix - All Along the Watchtower
</name>
</track>
</tracks>
<?php
function getTrackListings(){
$xml = new SimpleXMLElement('/path/to/tracks.xml');
$tracks = array();
foreach($xml->tracks as $track){
$tracks[ (int)$track->id ] = (string)$track->name;
}
return $tracks;
}
Thanks for that - I sadly have no understanding of php, i’ve put the php code in my html page as it said in your code and changed the top part to read the xml file and i have got this at the top of my page:
tracks as $track){ $tracks[ (int)$track->id ] = (string)$track->name; } return $tracks; } function sendTrackRequest($track){ return mail( ‘sean@1radio.org’, ‘Track Request’, sprintf(‘Please play %s, lots of love, a user.’, $track) ); } if(‘POST’ === $_SERVER[‘REQUEST_METHOD’]){ $track_id = filter_input(INPUT_POST, ‘track_id’); $tracks = getTrackListings(); if(array_key_exists($track_id, $tracks)){ sendTrackRequest($tracks[$track_id]); } } ?>
so is the below code right
<?php
function getTrackListings(){
$xml = new SimpleXMLElement(‘Music.xml’);
$tracks = array();
foreach($xml->tracks as $track){
$tracks[ (int)$track->id ] = (string)$track->name;
}
return $tracks;
}
function sendTrackRequest($track){
return mail(
‘sean@1radio.org’,
‘Track Request’,
sprintf(‘Please play %s, lots of love, a user.’, $track)
);
}
Are you saving the file on your local computer? If so, that is almost certainly not a web server and you might need to investigate more in to that side of the technology. It’s either that, or finding someone to take care of the dirty side of the work for you.
If you are testing this on your local computer, you can easily set up a local server environment for testing purposes. Check out WAMP of XAMPP (Windows) or MAMP (Mac).
i’m getting there now thanks just one problem (could be to do with my web host) but i am getting this message
Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /home/vhosts/paperlessstudio.eu5.org/1radio/request.php:3 Stack trace: #0 /home/vhosts/paperlessstudio.eu5.org/1radio/request.php(3): SimpleXMLElement->__construct(‘Music.xml’) #1 /home/vhosts/paperlessstudio.eu5.org/1radio/request.php(36): getTrackListings() #2 {main} thrown in /home/vhosts/paperlessstudio.eu5.org/1radio/request.php on line 3