Hey guys, I'm trying to output the contents of a text file onto the body of the page after a button click. After that, I want to write to the text file with a form and then output the contents once again. I think I'm almost there, but the code isn't working. Can you point out what I'm doing incorrectly? Thanks.

<?php
echo "<u><b>Song List</b></u><br />";
$list = "songList.txt";
//song list
$data = "Imagine"."<br />"."Born to Run"."<br />"."Ain't No Sunshine"."<br />"."Hey Jude"."<br />"."Hotel California"."<br />"."Layla"."<br />"."Stairway to Heaven"."<br />"."What's Going On"."<br />"."Yesterday"."<br />"."Heartbreak Hotel"."<br />";
//fopen to open file, 2nd argument 'w' to write
$open = fopen($list, 'w');
fwrite($open, $data);
fclose($open);
//return $list;

//on clicking view button
if (isset($_POST['view'])) {
$output_view = file_get_contents($list);
echo $output_view; }

//appending new songs
if (isset($_POST['newSong']) && isset($_POST['submit'])){
	$append = fopen($list, 'a');
	$newdata = $_POST['newSong'];
	fwrite($append, $newdata);
	fclose($append);
	readfile($list);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body>
----------------------------------------
<form action="" method="POST">
<input type="button" name="view" value="View List">
<input type="button" name="sort" value="Sort List">
<input type="button" name="shuffle" value="Shuffle List"><br /><br />
<input type="text" name="newSong" value="Insert Song Title">
<input type="button" name="submit" value="Add Song">
</form>
</body>
</html>

    sounds like a job for a db. however what specificity are you have a problem with ?

      Hey, the problem is that it's not outputting the contents of the text file onto the body of the page.

        Since you do not output it in the body of the page, it will not display in the body of the page.

        wrong

        Line A is supposed to be displayed between line B and line C - but it isn't...
        Line B
        Line C
        

        right

        Line B
        Line A is supposed to be displayed between line B and line C - and it is!
        Line C

        Also, validate your document

          I'm sorry. I don't follow. =\ This is my first php class so I'm very stuck, and wasn't able to get help because of Veteran's Day. I'm still very confused about when to use the return command, and in which cases echo does not work inside functions, etc.,

            Probably your main problem at the moment is that an <input> of type "button" does not submit anything -- it is fairly useless unless you combine it with some JavaScript and an onclick event. Try changing all of your type="button" attributes to type="submit", and then go from there. (As mentioned above, you'll probably want to move most/all of that PHP code down into the <body> section of the XHTML text, at the point where you want its output to appear in the document.)

              Thank you so much. I had no idea I was making such a careless mistake. I only looked at my php, and didn't bother to look at my form code. =\ It's working now!

                Write a Reply...