You are here: Re: Grabbing checkbox values « PHP Programming Language « IT news, forums, messages
Re: Grabbing checkbox values

Posted by Jerry Stuckle on 11/12/06 23:26

Amer Neely wrote:
> flamer die.spam@hotmail.com wrote:
>
>> Amer Neely wrote:
>>
>>> 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.
>>>
>>> Does each checkbox name have to be unique? I was hoping to just group
>>> them under one name, and select from that array.
>>>
>>> PHP is not my native language - I'm coming at this from Perl, so bear
>>> with me, things are a little different in that country.
>>>
>>> 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]")?>">&nbsp;<?php echo("$allfiles[$i]")?><br>
>>
>>
>> in html, if your checkbox is called "blah123" then when its checked it
>> will have the variable name of blah123 with a value of blah123, if it
>> is unchecked then blah123==null. the easy way is:
>>
>> if (!empty($_POST['blah123'])) { do stuff }
>>
>> Flamer.
>>
>
> Hmmm. OK, so I will need to also walk through each of the new input
> names, and check them with the 'empty' function. I'll work on that. Thanks.
>
>

Amer,

No. It's much easier than that. Have all the checkboxes names the
same, with square brackets ( "[]" ) following. The you will be able to
use an array on the PHP side, i.e.


<input type="checkbox" name="DeleteThis[]" value="<?php
echo("$allfiles[$i]")?>">&nbsp;<?php echo("$allfiles[$i]")?><br>>


To process the input, all you need is:

foreach ($_POST['DeleteThis'] as $delfile) {
// Validate the filename is OK here
unlink $delfile;
}

This will work whether the checkbox is set via a mouse click or javascript.

Of course, you will want to validate your filenames. That part I left
out because I have no idea what the criteria is.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация