I'm trying to figure out how to send checked options from a series of checkboxes to a text file that keeps track of what has been selected. The most I have right now is this.
<html>
<?php
//Get contents from zmzonSongs.txt file, put into array.
$songList = explode("
", file_get_contents('zmzonSongs.txt'));
print "Welcome to Zmzon. Select songs below to add to your library.";
//Print contents as checklist.
foreach($songList as $songs){
echo "<br/><input type='checkbox' name='songList[]' value='$songs' />"$songs<br>";
}
?>
</html>
I need to send the values of everything that has been checked to a file like myLibrary.txt after the user hits a submit button, but every time I try adding a submit button using the echo command, my entire page stops working and comes up blank. I'm completely lost.
You have an extra quote:
echo "<br/><input type='checkbox' name='songList[]' value='$songs' />"$songs<br>";
should be
echo "<br/><input type='checkbox' name='songList[]' value='$songs' />$songs<br>";
or
echo "<br/><input type='checkbox' name='songList[]' value='$songs' />".$songs."<br>";
Your checkboxes and submit button should be inside a form and your submit-button is HTML, not PHP.
<?php
/* Lets set the destination to the same file as the form. (see form action)
* That means, if someone has submitted the form, there will be data inside
* $_POST right now!
*/
// var_dump( $_POST ); <-- Can be used to quickly peek inside $_POST
if ( !empty( $_POST ) ) // Check if there is any data
{
save_data_to_file( $_POST ); // pseudo-code (should be replaced by you)
echo "success!"; // A success message perhaps?
exit(); // Stop here to not display the form below.
}
// If we come this far, it means the form has not been submitted.
// So lets display the form!
// You can have this line first in the file, or later,
// as long as you do this before you try to use it in your checkboxes.
$songList = explode("
", file_get_contents('zmzonSongs.txt'));
?>
<h1>Welcome to Zmzon. Select songs below to add to your library</h1>
<form action="thisfile.php" method="POST">
<?php
// See, you can jump between PHP and HTML whenever you want.
foreach($songList as $songs){
echo "<input type='checkbox' ... ><br>";
}
?>
<input type="submit">
</form>
You can send the data to another page or the same page, decided by "action". The data will be inside a variable $_POST or $_GET (depending on what sending method you choose.)
An easy way to look at those global variables is to use var_dump(). For example:
<pre>
<?php var_dump($_POST); ?>
updated again
<html>
<form method='post'>
<?php
//Get contents from zmzonSongs.txt file, put into array.
if (!isset ($_POST['songList']))
{
$songList = explode("
", file_get_contents('zmzonSongs.txt'));
print "Welcome to Zmzon. Select songs below to add to your library.";
//Print contents as checklist.
foreach($songList as $songs){
echo "<br/><input type='checkbox' name='songList[]' value='$songs' />"$songs<br>";
}
}
else
{
$songlist = $_POST['songList']; // which is array
// next you write code to update in text file.
}
?>
<input type="Submit" value="Submit">
</form>
</html>