|
Posted by Amer Neely on 11/13/06 16:27
Jerry Stuckle wrote:
> Amer Neely wrote:
>> Jerry Stuckle wrote:
>>
>>> Amer Neely wrote:
>>>
>>>> Jerry Stuckle wrote:
>>>>
>>>>> Amer Neely wrote:
>>>>>
>>>>>> Jerry Stuckle wrote:
>>>>>>
>>>>>>> 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]")?>"> <?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]")?>"> <?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.
>>>>>>>
>>>>>>
>>>>>> OK, I have your code inserted, but it gives me an error when I use
>>>>>> my 'Select All' JS:
>>>>>>
>>>>>> Warning: Invalid argument supplied for foreach() in show_spam4.php
>>>>>> on line 116
>>>>>>
>>>>>> If I click any box with the mouse it returns 'on' for the output.
>>>>>>
>>>>>> [116] foreach ($_POST['DeleteThis'] as $value)
>>>>>> {
>>>>>> echo "$value<br>";
>>>>>> }
>>>>>>
>>>>>
>>>>> Amer,
>>>>>
>>>>> I didn't mean for that to be the only code. Rather it was a guide
>>>>> to get you going in the right direction.
>>>>>
>>>>> The invalid argument is probably because none of the checkboxes are
>>>>> checked, so you're not getting anything in your PHP code. To make
>>>>> sure you're getting good information, check out isset() and
>>>>> isarray() functions.
>>>>>
>>>>
>>>> :) Sorry, I'm new with PHP so everything is literal. But as I
>>>> mentioned, if I check all the boxes with JS, and submit the form, it
>>>> throws that error. I'm going to do some reading and see what I can
>>>> come up with. Thanks for the input and direction so far.
>>>>
>>>
>>> I would suggest your javascript isn't doing it's job. When I check
>>> boxes with js the do appear in PHP.
>>>
>>
>> That isn't the problem - they appear just fine in the page. I can
>> toggle them off and on all night.
>>
>> It's only when the page is submitted that the values don't seem to be
>> getting through.
>>
>
> Then you need to check your javascript code. It doesn't make any
> difference to PHP whether the checkbox is checked by the user or JS - in
> fact, there is no way for PHP to tell.
>
> If the box is checked, the client sends it to the server when the form
> is submitted. If the box is not checked, the client does not submit it
> to the server. There is no "checked by user" or "checked by javascript"
> flag. It's either checked or not checked.
>
> And the fact it isn't going to the browser indicates it's not checked,
> or you've done something else with your javascript.
>
See my reply to Deniel Keplar earlier. My JS code is in there, and how I
use it. If you can implement it in some test code and see what PHP does
with it that would help I think.
I think I need to just play with some code and check whether a checkbox
'isset' or not, or whether it's 'on' or not.
--
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]
|