Question

I have a form that has a drop down list of song titles. I want to be able to click on one of the song titles and the lyrics be loaded on the same page. The lyrics are contained within .txt files in a folder called "Lyrics".

Example:

Drop down list contains: Song1 Song2 Song3 etc.

When the user clicks on the song, the contents of the corresponding .txt file are displayed on that same page. Any ideas?

Was it helpful?

Solution

<?php
    $song = intval( $_GET[ 'song_id' ] );
    $songs = array( 0 => NULL, 1 => 'Song1', 2 => 'Song2', 3 => 'Song3' );
    echo file_get_contents( $songs[ $song ] . '.txt' );
?>

Good Luck!

OTHER TIPS

It would be easier using JQuery:

<a href="somefile.txt" class="link">Song 1</a>
<pre id="lyrics">
</pre>

<script>
$(function(){
   $('.link').click(function(){
     $.get(this.href,null,function(lyrics){
         $('#lyrics').html(lyrics);
         return false;
     });
   });
});
</script>

If you have a list of songs, and you use a simple naming convention then use that as a white-list to check the GET value is permitted before you even think of using

file_get_contents

    $song = $_GET['song'] ;
    $file = '/Lyrics/' . $song . '.txt' ; 
    $songs = array( 'help', 'she-loves-you', 'yellow-submarine' );

   // as a double check see if the file exists too

   if( in_array( $song, $songs ) && file_exists( $file ) ) {
       echo file_get_contents( $file );
    }

If you are smart you can also then reuse the same $songs array to create your drop-list.

< option value= yellow-submarine > Yellow Submarine < / option>

And then if you keep this in a database, well you might decide that the 'slug' 'yellow-submarine' would make an ideal Private Key, but don't get me started on that ...

This is quick solution, but highly insecure, use it only as example, how could be done, but shouldn't:

<?

// your page code here // this is body where you want to put song lyrics

file_get_contents($_GET['songName'].".txt")

// else..

?>

in meniu:

<a href="songs.php?songName=song1">song1</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top