(Hopefully!) Simple Question about using checkboxes in PHP for Loops

Hi there,

I am relatively new to PHP and SQL so hopefully this is something really easy to do!

I have a for loop in PHP which echoes a database row, echoes a checkbox, then
. I have the value of the checkboxes set to a php variable when they’re printed (the ID of the row) and if I inspect element in Chrome I can see that they do indeed have this value. My question is how do I work with these values? I need to be able to store the selected checkbox values(id’s) to an array so I can then add the these items to another database. For reference this is a school project where music tracks are echoed, then the checkboxed tracks are added to the playlist database. Attached is the code. Thanks in advance!


help!.zip (438 Bytes)

Take the attached file, and just post the code in our code tags. Then, we will help.

[php]<?php
if(!empty($trackName)){
for($i = 0;$i < count($trackName); $i++ ){
echo “” . $trackName[$i] . “” . " | " . $trackDuration[$i] . " Mins " . " ". “
”;
}
}
?>

Add To Playlist <?php if(isset($_POST['btnAddToPlaylist'])){ if(!empty($_POST['checkbox'])) { foreach($_POST['checkbox'] as $value){ echo "value : ".$value.'
'; } }

}
?>
[/php]

Ooops sorry! Should be able to see in comments now. :slight_smile:

So, you are just looking to add those values to an array?
[php]
// define an array
$playlist = [];

// add a value to the array
array_push($playlist, $value);[/php]

Hi astonecipher,

Thanks so much for getting back to me - can you spot anything wrong with the below? I added an echo just so I could test if the checkbox values were stored in the array but sadly no luck :frowning: no PHP error messages either.

[php] <?php
if(isset($_POST[‘btnAddToPlaylist’])){
if(!empty($_POST[‘checkbox’])) {
foreach($_POST[‘checkbox’] as $value){
$selectedTracks = [];
array_push($selectedTracks,$value);
echo $selectedTracks[0];
}
}
}
?>[/php]

You are creating a new array each iteration, rather than adding to an existing one.

The if statement is unnecessary. If nothing is there, the loop will never run.

Ah thanks for spotting that! I have changed to the following code, but sadly still no luck :frowning: no errors or echoed track IDs.

[php] <?php
$selectedTracks = [];
if(isset($_POST[‘btnAddToPlaylist’])){
if(!empty($_POST[‘checkbox’])) {
foreach($_POST[‘checkbox’] as $value){
array_push($selectedTracks,$value);
}
echo $selectedTracks[0];
}
}
?>[/php]

I’m also little unsure about your comment on my if statement. My understanding was I needed the first IF statement so the code runs when the button is pressed and the second IF statement checks to see if any tracks (checkboxes) are selected?

You handle that by checking if the form was submitted to begin with:

[php]
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’)
{
// form processing is in here.
}
[/php]

Now, rather than go through a loop, you can make this cleaner. If you had other processing, I would not suggest this, but you are just adding to another array anyway at this point.

[php]
$selectedTitles = [];
if(!empty($_POST[‘checkbox’]))
$newArr = array_merge($_POST[‘checkbox’], $selectedTitles);

// Just to check if the array has values so no warnings show
if(!empty($newArr))
print_r($newArr);

[/php]

[php] <?php
if(!empty($trackName)){
for($i = 0;$i < count($trackName); $i++ ){
echo “” . $trackName[$i] . “” . " | " . $trackDuration[$i] . " Mins " . " ". “
”;
}
}
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’)
{
$selectedTracks = [];
if(!empty($_POST[‘checkbox’])){
$newArr = array_merge($_POST[‘checkbox’], $selectedTitles);
}
if(!empty($newArr)){
print_r($newArr);
}
else{
echo “No Tracks Added”;
}
}
?>[/php]

Hi astonecipher,

Can you spot anything wrong with the code above? Sadly it still doesn’t print out the checked values. I get “No Tracks Added”, so I guess it’s an issue with the array_merge()?

[php] $selectedTracks = [];
if(!empty($_POST[‘checkbox’])){
$newArr = array_merge($_POST[‘checkbox’], $selectedTitles);[/php]

$selectedTitles != $selectedTracks

Have you done a print_r($_POST) yet to see what comes in?

Ah thanks for spotting that! I swear I’m going crazy from looking at this code.

I changed both variables so they’re the same but still no IDs echoed. I get the below when I [php]print_r($_POST)[/php]

Array ( [btnAddToPlaylist] => )

Which means, those checkboxes are not in a form… That is your issue.

How can I put them in a form if the checkbox is being echoed? I had the html for the form below the php code and between the div tags so I guess they’re not linked?

What is in the form, is only what is between the tags.

There are other ways to do this, like javascript, but then you loose out on what you are trying to learn as well.

One way to do it, rather than echoing the checkboxes at the time, is to add them to a string or array and then echo that variable out in the form.

[php]$checkboxes = [];
for($i = 0; $i<10; $i++)
{
$checkboxes[] = “{$i}”;
}

<?php echo implode("
", $checkboxes); ?> [/php]

astonecipher thank you so much! That is working perfectly.

You have been amazing, thanks for putting up with me and having the patience

Sponsor our Newsletter | Privacy Policy | Terms of Service