Home ยป Php ยป Grabbing checkbox values – PHP

Grabbing checkbox values – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

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>


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!"

A(Answer):

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>

I’ve changed the code for the above line so the name now is unique for
each element. That now lets me grab whichever one/s are checked. But,
another problem has arisen.

I also have a JavaScript toggle to ‘Select/Unselect All’ checkboxes,
which works fine. But the ‘checked’ status doesn’t seem to be getting
passed to the PHP code. So, when I display the items checked, it is
incorrect. Refreshing the page also doesn’t seem to correct it. Only
physically clicking the checkboxes seems 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!"

A(Answer):

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>


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!"

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.

A(Answer):

php is not to "grab" the checkboxes, it comes and goes as lessons for
the yum mathematical calus on webpage stuff!
Use the html to expose your own web pages!

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>


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!"

A(Answer):

flamer di******@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 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!"

A(Answer):

flamer di******@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>


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!"

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.

I can now grab the selected elements, but only if they are selected by
mouse clicks. If I use the JavaScript toggle to select/unselect all,
nothing gets passed to the PHP. This works in Perl, so it has to be
doable in PHP I would think.


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!"

A(Answer):

..oO(flamer di******@hotmail.com)

>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

It will have the value of the ‘value’ attribute.

>if it
is unchecked then blah123==null.

If unchecked then the checkbox is not submitted at all.

Micha

A(Answer):

..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]")?>">&nbsp;<?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’>&nbsp;%s</label><br>\n", $i, $allfiles[$i]);

HTH
Micha

A(Answer):

script, not html, this way post becomes fully clear.

Rogats wrote:

php is not to "grab" the checkboxes, it comes and goes as lessons for
the yum mathematical calus on webpage stuff!
Use the html to expose your own web pages!

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>


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!"

A(Answer):

after you succeed, post your reply, I want to know how also.

Amer Neely wrote:

>
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.

A(Answer):

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]")?>">&nbsp;<?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’>&nbsp;%s</label><br>\n", $i, $allfiles[$i]);

HTH
Micha

I can get the checked boxes only if the user clicks them with the mouse.
Which is fine, but I also have some JavaScript that lets me toggle the
boxes ‘select / unselect all’. This is the part that is now giving me
grief. In Perl I can do:

my @infiles=param(‘DeleteThis’);

which gets an array of all checked entries in the ‘DeleteThis’ group.
And this works with my JavaScript code. The ‘param’ function is part of
a perl module (CGI.pm) that deals with form parsing. Is there anything
like that for PHP? It would make life so much easier.


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!"

A(Answer):

Amer Neely wrote:

flamer di******@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.
js*******@attglobal.net
==================

A(Answer):

Deniel Keplar wrote:

after you succeed, post your reply, I want to know how also.

Amer Neely wrote:

>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.

Will do.

In the mean time, here is the JS code I use to toggle checkboxes on or
off. You will have to edit to suit your situation. Specifically, this is
hard-coded to work on the first form on a page (forms[0]) so you would
change that to either a form name or specify which form you want to work
with.

function CheckAll(value) // toggles checkboxes on/off
{
for (i=0; i < document.forms[0].length; i++)
{
document.forms[0].elements[i].value);
if ( document.forms[0].elements[i].name =~ ‘DeleteThis’)
{
document.forms[0].elements[i].checked=value;
}
}
}

The function is called like this:
<a href="#" style="color:red; text-decoration:none"
onClick="CheckAll(1);return false;">Select All</a>&nbsp;•&nbsp;<a
style="color:red; text-decoration:none" href="#" onClick="CheckAll(0);
return false;">Unselect All<br /></a>


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!"

A(Answer):

Jerry Stuckle wrote:

Amer Neely wrote:

>flamer di******@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.

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 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!"

A(Answer):

Deniel Keplar wrote:

after you succeed, post your reply, I want to know how also.

Amer Neely wrote:

>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.

Found a couple of good articles here:
http://www.webreference.com/programming/php/

Follow Part 1 and Part 2.


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!"

A(Answer):

Amer Neely wrote:

Jerry Stuckle wrote:

>Amer Neely wrote:

>>flamer di******@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.

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.


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

A(Answer):

Jerry Stuckle wrote:

Amer Neely wrote:

>Jerry Stuckle wrote:

>>Amer Neely wrote:

flamer di******@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.

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.


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!"

A(Answer):

Amer Neely wrote:

Jerry Stuckle wrote:

>Amer Neely wrote:

>>Jerry Stuckle wrote:

Amer Neely wrote:

flamer di******@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.
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.


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

A(Answer):

Jerry Stuckle wrote:

Amer Neely wrote:

>Jerry Stuckle wrote:

>>Amer Neely wrote:

Jerry Stuckle wrote:

Amer Neely wrote:
>
>flamer di******@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.
>

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.


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!"

A(Answer):

Amer Neely wrote:

Jerry Stuckle wrote:

>Amer Neely wrote:

>>Jerry Stuckle wrote:

Amer Neely wrote:

Jerry Stuckle wrote:
>
>Amer Neely wrote:
>>
>>flamer di******@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.
>>
>
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.


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

A(Answer):

..oO(Amer Neely)

>Jerry Stuckle wrote:

>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.

I already posted some code similar to Jerry’s, but with the required
isset() check to avoid the error message you got.

Micha

A(Answer):

..oO(Amer Neely)

>:) 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.

There’s an error in your script. Try this one (tested, works here):

function CheckAll(value) {
for (i=0; i<document.forms[0].length; i++) {
if (document.forms[0].elements[i].name = ‘DeleteThis[]’) {
document.forms[0].elements[i].checked=value;
}
}
}

Micha

A(Answer):

..oO(Amer Neely)

>:) 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.

There’s an error in your script. Try this one (tested, works here):

function CheckAll(value) {
for (i=0; i<document.forms[0].length; i++) {
if (document.forms[0].elements[i].name == ‘DeleteThis[]’) {
document.forms[0].elements[i].checked=value;
}
}
}

Micha

A(Answer):

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 di******@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.
>>>
>>
>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!"

A(Answer):

Michael Fesser wrote:

.oO(Amer Neely)

>:) 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.

There’s an error in your script. Try this one (tested, works here):

function CheckAll(value) {
for (i=0; i<document.forms[0].length; i++) {
if (document.forms[0].elements[i].name == ‘DeleteThis[]’) {
document.forms[0].elements[i].checked=value;
}
}
}

Micha

When you say it ‘work’s, does that mean your PHP tells you upon form
submission which boxes were checked? Mine works fine IF AND ONLY IF I
check the boxes with the mouse, even though the JS code will toggle them
on or off perfectly.

I had been playing with name of the field. First it was hard-coded to
‘DeleteThis’, then a suggestion was that it should be an array, so I
changed it to ‘DeleteThis[]’. That didn’t seem to work, so I went back
to ‘DeleteThis’, and changed the code to this:
if ( document.forms[0].elements[i].name =~ ‘DeleteThis’)
to catch everthing.


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!"

A(Answer):

..oO(Amer Neely)

>When you say it ‘work’s, does that mean your PHP tells you upon form
submission which boxes were checked?

Yep. The problem with your script was the operator ‘=~’, which doesn’t
exist in JS. Use a ‘==’ and it works.

>Mine works fine IF AND ONLY IF I
check the boxes with the mouse, even though the JS code will toggle them
on or off perfectly.

The problem was this part:

document.forms[0].elements[i].name =~ ‘DeleteThis’

‘=~’ is not an operator recognized by JS. It is interpreted as an
assignment operator, followed by a bitwise negation of its operand!

I don’t know for sure what the JS engine does if you try to negate a
string, but it look as if it’s casted to an integer first, which is 0 in
this case. After bitwise negation it became -1 (two’s complement), which
then got assigned to each control’s name. A simple

var_dump($_POST);

at the beginning of the PHP script shows that.

Micha

A(Answer):

Michael Fesser wrote:

.oO(Amer Neely)

>When you say it ‘work’s, does that mean your PHP tells you upon form
submission which boxes were checked?

Yep. The problem with your script was the operator ‘=~’, which doesn’t
exist in JS. Use a ‘==’ and it works.

>Mine works fine IF AND ONLY IF I
check the boxes with the mouse, even though the JS code will toggle them
on or off perfectly.

The problem was this part:

document.forms[0].elements[i].name =~ ‘DeleteThis’

‘=~’ is not an operator recognized by JS. It is interpreted as an
assignment operator, followed by a bitwise negation of its operand!

I don’t know for sure what the JS engine does if you try to negate a
string, but it look as if it’s casted to an integer first, which is 0 in
this case. After bitwise negation it became -1 (two’s complement), which
then got assigned to each control’s name. A simple

var_dump($_POST);

at the beginning of the PHP script shows that.

Micha

ARghhh! That’s my perl coming through. ‘=~’ is a common regex operator
for matching strings. In JS I’ll have to define a variable with my pattern.


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!"

A(Answer):

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]")?>">&nbsp;<?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’>&nbsp;%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]")?>">&nbsp;<?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!"

A(Answer):

Amer Neely wrote:

Jerry Stuckle wrote:

>>
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,

If you change the name to DeleteThis[], you’ve also got to change the
JavaScript code to reflect the name.

And Michael also pointed out the other error in your code. Use his code
– it works fine.

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

A(Answer):

Jerry Stuckle wrote:

Amer Neely wrote:

>Jerry Stuckle wrote:

>>>
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,

If you change the name to DeleteThis[], you’ve also got to change the
JavaScript code to reflect the name.

And Michael also pointed out the other error in your code. Use his code
– it works fine.

I’ve made a few changes in both the JS and PHP, and have solved the
problem (see my latest post under this subject heading) :: SOLVED.

Again thanks for the input and insight.


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!"