Jump to content

[SOLVED] Need help with a form - multiple checkboxes


jsschmitt

Recommended Posts

This is the usage:

Music Player w/ Playlists

 

This is the issue:

I am building a music player, that allows the creation of playlists. The issue lies in reading from the form when multiple songs are selected.

 

This is the method:

createplist.php contains a form that is built using a directory script. When the tracks are selected, and submitted, create.php makes a .xml file that contains the list of tracks.

 

This is the code:

createplist.php

<?php
		/* $url = "http://www.anomymage.com/tests/do/music/"; */
		$path = "music/";
		print "<form method='post' action='create.php'>\n";
		print "<input type='text' name='plistname' value='Playlist Name' /><br/>\n";

		$narray=array();
		$dir_handle = @opendir($path) or die("Unable to open $path");
		$i=0;
		while($file = readdir($dir_handle))
		{
		if($file != '.' && $file != '..' )
		{
		//echo "<a href='$path/$file'>$file</a>";
		$narray[$i]=$file;
		$i++;
		}
		}
		sort($narray);

		for($i=0; $i<sizeof($narray); $i++)
		{
		echo "<input type='checkbox' name='song' value='".$narray[$i]."'>".$narray[$i]."</input><br/>\n";

		}

		//closing the directory
		closedir($dir_handle);

		print "<input type='submit' value='>'>\n";
		print "</form>\n";
	?>

 

create.php

		<?php
		$name = $_POST['plistname'];
		$begin = "<xml><track><path>http://www.anomymage.com/tests/do/music/music/";
		$path = $_POST['song'];	    
		$mid = "</path><title>";
		$title = $_POST['song'];
		$end = "</title></track></xml>";


		$file = "playlists/".$name.".xml";
		$Saved_File = fopen($file, 'w');
		fwrite($Saved_File, $begin.$path.$mid.$title.$end);
		fclose($Saved_File);
		echo "Playlist <strong>".$name."</strong> created.<br />
			<a href='../music/'>Back</a>";
	?>

 

Link to comment
Share on other sites

It would be great to know what the issue actually is. What happens when multiple songs are selected?

 

Looking at your second code page, my guess is that you're going to need a loop to loop through all the selected songs, since all it looks like is that it's taking just the first song.

Link to comment
Share on other sites

It would be great to know what the issue actually is. What happens when multiple songs are selected?

 

Looking at your second code page, my guess is that you're going to need a loop to loop through all the selected songs, since all it looks like is that it's taking just the first song.

 

Being a noob and all... any help on how to do that?

Link to comment
Share on other sites

Well, maybe I'm just missing how you're doing it... but if you had multiple checkboxes, wouldn't you assume you need a loop of some sort in create.php to loop through and get the value from each checkbox?

 

For instance... if "song" is the checkboxes, something like

$begin = "<xml><track>";
$name = $_POST['plistname'];
foreach ($_POST['song'] as $v) {

$path = $_POST['song'];       
$mid = "<path>http://www.anomymage.com/tests/do/music/music/"</path><title>";
$title = $_POST['song'];
$titleclose = "</title>";

}

$end = "</track></xml>";

 

Link to comment
Share on other sites

I would suggest setting the name of the input to :

 

<input type='checkbox' name='song[]' value='".$narray[$i]."'>".$narray[$i]."</input>

 

So it would be an array when You submit it.

 

Also when You get the variable on the other side

 

$song_array = $_POST['song'];

for ($i=0; $i < count($song_array); $i++)
{
// print out the xml
}

Link to comment
Share on other sites

Updated code below, but now i get the following error:

 

Warning: fwrite(): 3 is not a valid stream resource in /home/jschmitt/public_html/anomy/tests/do/music/create.php on line 29

 

Warning: fclose(): 3 is not a valid stream resource in /home/jschmitt/public_html/anomy/tests/do/music/create.php on line 30

 

<?php

		$song_array = $_POST['song'];

		$name = $_POST['plistname'];
		$bxml = "<xml>";

		$file = "playlists/".$name.".xml";
		$Saved_File = fopen($file, 'a');
		fwrite($Saved_File, $bxml);
		fclose($Saved_File);

		for ($i=0; $i < count($song_array); $i++)
		{

			$begin = "<track><path>http://www.anomymage.com/tests/do/music/music/";
			$path = $song_array[$i];   
			$mid = "</path><title>";
			$title = $song_array[$i];
			$end = "</title></track>";
			$content = $begin.$path.$mid.$title.$end;

			fwrite($Saved_File, $content);
			fclose($Saved_File);
		};

		$exml = "</xml>";
		fwrite($Saved_File, $exml);
		fclose($Saved_File);

		echo "Playlist <strong>".$name."</strong> created.<br />
				<a href='../music/'>Back</a>";
	?>

Link to comment
Share on other sites

Nevermind... fixed that as well..

 

Full code (that now works) below:

 

		<?php

		$song_array = $_POST['song'];

		$name = $_POST['plistname'];
		$bxml = "<xml>";

		$file = "playlists/".$name.".xml";

		if (file_exists($file)) {
    				echo "The playlist ".$file." already exists";
		} else {
   			
		$Saved_File = fopen($file, 'a');
		fwrite($Saved_File, $bxml);
		fclose($Saved_File);

		for ($i=0; $i < count($song_array); $i++)
		{

			$begin = "<track><path>http://www.anomymage.com/tests/do/music/music/";
			$path = $song_array[$i];   
			$mid = "</path><title>";
			$title = $song_array[$i];
			$end = "</title></track>";
			$content = $begin.$path.$mid.$title.$end;

			$file = "playlists/".$name.".xml";
			$Saved_File = fopen($file, 'a');	
			fwrite($Saved_File, $content);
			fclose($Saved_File);
		};

		$exml = "</xml>";
		$file = "playlists/".$name.".xml";
		$Saved_File = fopen($file, 'a');
		fwrite($Saved_File, $exml);
		fclose($Saved_File);

		echo "Playlist <strong>".$name."</strong> created.<br />
				<a href='../music/'>Back</a>";};
	?>

Link to comment
Share on other sites

  • 3 months later...

Hey just wondering, you built your own music player or what did you use? This is exactly the kind of thing I've been trying to make for my website but have had no avail, I'm using Drupal, and modifications to try and make a social networking site, so it would help great deal. Thanks for any info

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.