By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Problems sending arrays by form when type=checkbox

P: n/a
Hi all, I have a problem when I try to send an array using a form when
the type="checkbox".

This is my form input row:

<INPUT type="Checkbox" name="flg[]" value="y" <? if($row['flag'] ==
'y') echo 'CHECKED'; ?>>

Since this is an update form, I set the status of the flg[] according
to the field "flag" stored in a MySQL database. The user may check or
uncheck each of the checkboxes on the form.

This form is sent to another PHP file with other fileds of type
<SELECT> and type="Text". Here's the code which process the checkbox
input:

$max_i = count($flg);
for($i=0;$i<$max_i;$i++)
{
if($flg[$i] == 'y')
{
$flag = 'y';
}
else
{
$flag = 'n';
}
echo $i.'-->'.$flg[$i].'-->'.$flag.'<BR>';
}

I have no problems processing the array associated with the SELECT
and "Text" fileds, but when I try using the Checkbox, this is what I
receive:

0-->y-->y
1-->y-->y
2-->y-->y
3-->y-->y
4-->y-->y
5-->y-->y

Trying with this other code:

$num_of_checkbox_fileds = 12;

for($i=0;$i<$num_of_checkbox_fileds;$i++)
{
if($flg[$i] == 'y')
{
$flag = 'y';
}
else
{
$flag = 'n';
}
echo $i.'-->'.$flg[$i].'-->'.$flag.'<BR>';
}

I receive this output:

0-->y-->y
1-->y-->y
2-->y-->y
3-->y-->y
4-->y-->y
5-->y-->y
6-->-->n
7-->-->n
8-->-->n
9-->-->n
10-->-->n
11-->-->n
The problem is that there is no order in what I receive!!
I have the first elements af the array with a 'y' and the others with
a 'n'.
The correct output, according with the user's input, should be:

0-->n-->n
1-->y-->y
2-->n-->n
3-->y-->y
4-->n-->n
5-->y-->y
6-->n-->n
7-->y-->y
8-->n-->n
9-->y-->y
10-->n-->n
11-->y-->y

Any idea? Please help!!!

Thank You.

Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Old Lady wrote:
I have a problem when I try to send an array using a form when
the type="checkbox".

This is my form input row:

<INPUT type="Checkbox" name="flg[]" value="y" <? if($row['flag'] ==
'y') echo 'CHECKED'; ?>>


Give each checkbox a different index

<input type="checkbox" name="flg[0]" ... />
<input type="checkbox" name="flg[1]" ... />
...

This way you can access them as a set in php ($_POST['flg']) or
individually ($_POST['flg'][2]).


BTW: Don't you want to turn register_globals off? :-)

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #2

P: n/a
>Give each checkbox a different index

<input type="checkbox" name="flg[0]" ... />
<input type="checkbox" name="flg[1]" ... />
...

This way you can access them as a set in php ($_POST['flg']) or
individually ($_POST['flg'][2]).
Yes, I already tryied this and it works, but I am trying to understand
why this problem is just with checkboxes and not with text or select
fields. Any idea? It's a HTML bug or what?
BTW: Don't you want to turn register_globals off? :-)


Yep... But the code is VERY old and I have to change a lot of rows in
a suite applications used by 2,500 users. I'm starting fixing the code
for the register_globals, but this will take A LOT of time, to fix and
to check for any new errors...
Jul 17 '05 #3

P: n/a
Don't know exactly what your are trying to do but:

When a user checks the checkbox, only the items checked show up as
array items based upon your form. So a possible 'no' related to an
unchecked box at position 6 will be overwritten with a checked 'yes'
from position 8 based upon the way you write your form, so you will not
have the correct resulting order at the end. When you later go through
and compare, your 'no's may not be in the correct order as a result.
You could change the value yes to a incrementing number. Then, when
you examine the array, you at least know which items belong where in
the output, and you write your output accordingly.

Any items that were checked mean yes to you, so it doesn't matter if
there 'value' is a number to help you order them later.

Scott

Old Lady wrote:
Give each checkbox a different index

<input type="checkbox" name="flg[0]" ... />
<input type="checkbox" name="flg[1]" ... />
...

This way you can access them as a set in php ($_POST['flg']) or
individually ($_POST['flg'][2]).
Yes, I already tryied this and it works, but I am trying to

understand why this problem is just with checkboxes and not with text or select
fields. Any idea? It's a HTML bug or what?
BTW: Don't you want to turn register_globals off? :-)
Yep... But the code is VERY old and I have to change a lot of rows in
a suite applications used by 2,500 users. I'm starting fixing the

code for the register_globals, but this will take A LOT of time, to fix and to check for any new errors...


Jul 17 '05 #4

P: n/a
.oO(Old Lady)
This way you can access them as a set in php ($_POST['flg']) or
individually ($_POST['flg'][2]).


Yes, I already tryied this and it works, but I am trying to understand
why this problem is just with checkboxes and not with text or select
fields. Any idea? It's a HTML bug or what?


It's not a bug, it's a feature (or simply the way HTML works).

17.13.2 Successful controls
http://www.w3.org/TR/html401/interac...html#h-17.13.2

Unchecked checkboxes are not considered successful and hence not
submitted to the server. Without explicitly indexing them the result
will be rather random and useless.

Micha
Jul 17 '05 #5

P: n/a
Old Lady wrote:
I am trying to understand
why this problem is just with checkboxes and not with text or select
fields. Any idea? It's a HTML bug or what?


By design (HTML) only form controls with data get sent to the server.
So, if you have

[X] check 1
[ ] check 2
[X] check 3

Only "check 1" and "check 3" will be sent to the server.
When the control names are equal and include "[]" php converts them to
an array.
The browser sends something like "flg[]; flg[]" and php converts
to flg[0] and flg[1].

If you include the indexes in the control name, the browser will send
"flg[0]; flg[2]" which will be interpreted literally by php.

And that means that if you do

<?php
for ($i=0; $i<3; ++$i) {
if ($_POST['flg'][$i]) /* something */;
}

you will get a notice about an undefined index for $_POST['flg'][1].

BTW: Don't you want to turn register_globals off? :-)


Yep... But the code is VERY old and I have to change a lot of rows in
a suite applications used by 2,500 users. I'm starting fixing the code
for the register_globals, but this will take A LOT of time, to fix and
to check for any new errors...


I don't envy you :-)
Good luck!

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #6

P: n/a
Thank You Scott and Michael for your answers.

This is what I needed: knowing why the input order was not respected.
This means that when one want to use checkboxes in an array, he/she
HAVE to think a workaround.
Actually the workaround which best solve my needs is to 'force' the
array items using flg[0], flg[1], ..., flg[n] directly in the input
form. This is a very easy task to do in PHP, so this is the workaround
I used.

I also used the other workaround proposed by Scott in another
procedure, since it was best for another kind, but similar,
application.

Thank You again for your fast answers! ;-)

Old Lady

Jul 17 '05 #7

P: n/a
I noticed that Message-ID:
<11*********************@z14g2000cwz.googlegroups. com> from scotty
contained the following:
When you later go through
and compare, your 'no's may not be in the correct order as a result.
You could change the value yes to a incrementing number.


Or even the name of the item

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.