|
Posted by Amer Neely on 11/13/06 19:15
Michael Fesser wrote:
> .oO(Amer Neely)
>
>> I've got a dynamically built form with checkboxes for each element ( a
>> list of file names in a directory). I need to grab only those checkboxes
>> that are checked, so I can then delete those files.
>
> Only checked checkboxes will be submitted by the browser.
>
>> Does each checkbox name have to be unique?
>
> No, you can use the same name, but different values. To make this work
> in PHP you have to add brackets to the name, see below.
>
>> I've tried:
>> foreach ($_REQUEST as $field => $value)
>> {
>> echo "$field = $value<br>";
>> }
>> but that just grabs the last entry in the group.
>>
>> The line in question is:
>> <input type="checkbox" name="DeleteThis" value="<?php
>> echo("$allfiles[$i]")?>"> <?php echo("$allfiles[$i]")?><br>
>
> Some things:
>
> 1) You should not use $_REQUEST. This array contains data from different
> sources, so you don't know where all the values actually came from. Use
> the array that corresponds with the used method in your form, which
> should be $_POST in this case.
>
> 2) Instead of $allfiles[$i] you can just use the variable $i as the
> value of your checkboxes, that's enough. When processing the form use
> that value to lookup the filenames in your $allfiles array.
>
> 3) To process multiple form controls that share the same name you have
> to add square brackets to their name, e.g. "DeleteThis[]". This causes
> PHP to store all the values in an array instead of a scalar.
>
> So the code could look something like this:
>
> if (isset($_POST['DeleteThis')) {
> foreach ($_POST['DeleteThis'] as $index) {
> if (isset($allfiles[$index])) {
> // perform action to delete the file
> }
> }
> }
>
> The code for the checkboxes like I would probably write it:
>
> printf("<label><index type='checkbox' name='DeleteThis[]'
> value='%u'> %s</label><br>\n", $i, $allfiles[$i]);
>
> HTH
> Micha
OK, I've got my JS / PHP working with the checkboxes finally.
I changed my checkbox definition so that each box has a unique name:
<input type="checkbox" name="DeleteThis_<?php echo("$i")?>" value="<?php
echo("$allfiles[$i]")?>"> <?php echo("$allfiles[$i]")?><br>
where: $i is a loop index; $allfiles is an array of filenames
I changed my JS to check for a 'checkbox' type rather than looking for a
name:
function CheckAll(value) // toggles checkboxes on/off
{
for (i=0; i < document.forms[0].length; i++)
{
if (document.forms[0].elements[i].type == 'checkbox')
{
document.forms[0].elements[i].checked=value;
}
}
}
Then in my PHP I check the boxes:
foreach ($_POST as $field => $value)
{
if (!empty($_POST[$field]))
{
echo "$value<br>";
}
}
Now I can go and delete only those files that are checked, whether by JS
or mouse-click.
My thanks to everyone for their input and direction. Another fine
example of how newsgroups are supposed to work.
--
Amer Neely
w: www.softouch.on.ca/
b: www.softouch.on.ca/blog/
Perl | MySQL programming for all data entry forms.
"We make web sites work!"
Navigation:
[Reply to this message]
|