Jump to content

[SOLVED] how to store checkbox values to an array?


s0c0

Recommended Posts

For my application I need to allow the user to select multiple mysql records via an html form checkbox.  On submission of the form I would like to store these values in an array for either multiple record deletion or creating a list of records.

 

Here is what the form code looks like:

 

/* loop throgh user's mp3s and create html table row for each */
for($i=0; $i<$num_results; $i++)
{
	$row = $result->fetch_assoc();
	echo "<tr>";
	echo "<td><input type='checkbox' name='track[]' value='". $row['id'] ."'></td> \n"; // radio value is mp3.id
	echo "<td><a href='" . $row['file'] . "'>" . $row['song'] . "</a></td> \n"; //link is mp3 file
	echo "<td>" . $row['artist'] . "</td> \n";
	echo "<td><a href=\"javascript:popUp('myupdate.php?id=". $row['id'] ."&uid=". $uid ."')\">Update</a></td> \n";
	//echo "<td>" . $row['genre'] . "</td> \n" ;
	//echo "<td>" . $row['playtime'] . "</td> \n";
	//echo "<td>" . $row['playlist'] . "</td>";
	echo "</tr> \n";
}
	echo "</table> \n";

 

Here is where the array code needs to be:

 

	if (isset($_POST['submit']))
	{


	/*
		$queryTrack = "SELECT * FROM mp3 WHERE id='$_POST[track]'";
		$delTrack = mysqli_query($db, $queryTrack);

		while ($row = mysqli_fetch_array($delTrack))
		{
			$file = $row['file'] ."<br/>";
			$fullpath = $docroot . $row['file'];
			unlink($fullpath);
		}

		$queryID = "DELETE FROM mp3 WHERE id='$_POST[track]'";
		$idResult = mysqli_query($db, $queryID);
		header("Location:mymusic.php");
	*/
	}

 

For the life of me I cannot figure out how to do this.  I've tried the following type stuff just to get a hang of it, but I get an error like Invalid argument supplied for foreach()

 

foreach($track as $tid)
{
     echo $tid;
}

Link to comment
Share on other sites

Okay I gave up on myself to early once again.  Here's the solution.

 

You still need to post your checkbox form data so php knows what your talking about.  In my case I would write the following near the top of my code with the rest of my variables:

 

$track = $_POST["track"];

 

That's how I got it too work.  My that's simple!

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.