-2

I have an assignment that requires me to create two mock music services, each with a small library of songs available for purchase. If I can figure out how to make one work, the rest should be simple for the most part. I managed to get checkboxes working ONCE in my file but I modified one thing VERY SLIGHTLY and now the whole thing refuses to work. What I need to know is how to generate several checkboxes using a text file (which I put into an array), save the selected choices, and put that into a new text file to be read by another .php thing. This is what I have so far.

<html>
<?php
//Get contents from zmzonSongs.txt file, put into array.
$songList = explode("\n", file_get_contents('zmzonSongs.txt'));

//Print songs to be selected.
foreach($songList as $songs){
    echo "<br/><input type='checkbox' name=\'$songList[]\' value='$songs />$songs<br>";
}
?>

</html>

I need help immediately. The assignment is due soon and I'm struggling to make this work. I need to make it so that the contents of the array can be written into some file after whichever songs the user wants are selected.

user3308219
  • 157
  • 2
  • 11

2 Answers2

0

I think maybe your name="$songList[]" doesn't need the $.

When sending check boxes from an HTML form to PHP array it is not required in my experience.

foreach($songList as $songs){
    echo "<br/><input type='checkbox' name='songList[]' value='$songs' />$songs<br>";
}
Enjabain
  • 181
  • 1
  • 8
0
<?php

$songs = explode("\n", trim("
Song #1
Song #2
Song #3
Song #4
Song #5
Song #6
Song #7
"));

print_r($songs);

while ($song = array_shift($songs)) {
    $song = trim(htmlentities($song));

    echo "
<input type='checkbox' name='songList[]' value='$song'/> $song
";
}

Produces:

Array
(
    [0] => Song #1
    [1] => Song #2
    [2] => Song #3
    [3] => Song #4
    [4] => Song #5
    [5] => Song #6
    [6] => Song #7
)

<input type='checkbox' name='songList[]' value='Song #1'/> Song #1

<input type='checkbox' name='songList[]' value='Song #2'/> Song #2

<input type='checkbox' name='songList[]' value='Song #3'/> Song #3

<input type='checkbox' name='songList[]' value='Song #4'/> Song #4

<input type='checkbox' name='songList[]' value='Song #5'/> Song #5

<input type='checkbox' name='songList[]' value='Song #6'/> Song #6

<input type='checkbox' name='songList[]' value='Song #7'/> Song #7

http://codepad.org/fK187pfz

And the file saving bit:

http://us3.php.net/function.file-put-contents

if (is_array($_POST['songList'])
     && !empty($_POST['songList'])) {
    $songList = implode("\n", $_POST['songList']);
    $songList = filter_var($songList, FILTER_SANITIZE_STRING);

    file_put_contents($songList, '/your/path/to/directory/songList.txt');
}

If you want to remove all songs, I'll leave that to you to work out. However, I would suggest you have a name attribute on your submit button...

Your instructor probably wants you to do the fopen()/fwrite()/fclose() process, though, so I would check the PHP manual for examples how to do that.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • I have ten songs in a .txt file and the code is meant to be more universal in the event that more songs are added to the text file. How would I go about doing this? – user3308219 Feb 13 '14 at 23:47
  • Replace the string within the `explode` with your call to `file_get_contents()`. codepad doesn't allow that function, so I can't demonstrate that. You also probably want to add `
    ` to the end of the `echo`'d line.
    – Jared Farrish Feb 13 '14 at 23:48
  • I would probably also use a `label` around it so it's easily clickable: http://codepad.org/GkhzIzUu Note as well I used `$song` instead of `$songList` for the row variable, since you get a `song` from a list of `songs`, and then the *checklist* that's selected is an actual `songList`. Properly naming your variables helps keep things from getting confusing. – Jared Farrish Feb 13 '14 at 23:49