p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: checkbox array values not coming through in order


Message #1 by "Trey Carroll" <treycarroll@y...> on Wed, 9 Oct 2002 23:07:16
I have a series of checkboxes on a form with this syntax

<form action='myphppage.php' action='post' name='xxx'>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
</form>





after the form is submitted to my php page I get the value like this:

$chkBox = $_POST['chkBox'];

when I loop through the elements:

for ($i=0;$i<=5;$i++) {echo "chkBox value=".$chkBox[$i]."<br>";}

I find that the order of the checked elements is not preserved.  If ANY 2 
chkboxes are checked - the first 2 elements of the array are 'on'.  If the 
last 3 are checked- the first 3 array elements are 'on'.  If the last cb 
is checked, the first array element is 'on', etc...

I sure would like to be able to use this to be able to see WHICH 
checkboxes were checked- right now all this is good for is telling me how 
many are checked.  

Is there a way to do this?
Message #2 by "Gerry Vandermaesen" <gerry@a...> on Thu, 10 Oct 2002 08:56:52 +0200
Checkboxes which are not checked are not passed by the form, so from
PHP's point of view these values don't exist. You could solve it by
using this code:

<form action='myphppage.php' action='post' name='xxx'>
<input type='checkbox' name='chkBox[0]' ...>
<input type='checkbox' name='chkBox[1]' ...>
<input type='checkbox' name='chkBox[2]' ...>
<input type='checkbox' name='chkBox[3]' ...>
<input type='checkbox' name='chkBox[4]' ...>
</form>

Or you could determine which checkboxes are checked by giving unique
values to them.

-----Original Message-----
From: Trey Carroll [mailto:treycarroll@y...] 
Sent: woensdag 9 oktober 2002 23:07
To: beginning php
Subject: [beginning_php] checkbox array values not coming through in
order


I have a series of checkboxes on a form with this syntax

<form action='myphppage.php' action='post' name='xxx'>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
</form>





after the form is submitted to my php page I get the value like this:

$chkBox = $_POST['chkBox'];

when I loop through the elements:

for ($i=0;$i<=5;$i++) {echo "chkBox value=".$chkBox[$i]."<br>";}

I find that the order of the checked elements is not preserved.  If ANY
2 
chkboxes are checked - the first 2 elements of the array are 'on'.  If
the 
last 3 are checked- the first 3 array elements are 'on'.  If the last cb

is checked, the first array element is 'on', etc...

I sure would like to be able to use this to be able to see WHICH 
checkboxes were checked- right now all this is good for is telling me
how 
many are checked.  

Is there a way to do this?

Message #3 by "Trey Carroll" <treycarroll@y...> on Tue, 15 Oct 2002 17:02:10
Thanks for the reply Gerry

Unfortunately your suggestion seems to produce the same result that I was 
getting before. 

I just wound up doing it the long way: naming each checkbox chkBox0, 
chkBox1, chkBox2 ... and then putting them into an array by hand.  

I just learned about passing in an array by adding the [] to html form 
variables and wanted to experiment with it.  It *seemed like* it was a 
very efficient way to do things, but I guess it's only good for special 
situations (where you want to know how MANY values you have, not to be 
able to inspect INDIVIDUAL values).

Trey Carroll


> Checkboxes which are not checked are not passed by the form, so from
PHP's point of view these values don't exist. You could solve it by
using this code:

<form action='myphppage.php' action='post' name='xxx'>
<input type='checkbox' name='chkBox[0]' ...>
<input type='checkbox' name='chkBox[1]' ...>
<input type='checkbox' name='chkBox[2]' ...>
<input type='checkbox' name='chkBox[3]' ...>
<input type='checkbox' name='chkBox[4]' ...>
</form>

Or you could determine which checkboxes are checked by giving unique
values to them.

-----Original Message-----
From: Trey Carroll [mailto:treycarroll@y...] 
Sent: woensdag 9 oktober 2002 23:07
To: beginning php
Subject: [beginning_php] checkbox array values not coming through in
order


I have a series of checkboxes on a form with this syntax

<form action='myphppage.php' action='post' name='xxx'>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
<input type='checkbox' name='chkBox[]' ...>
</form>





after the form is submitted to my php page I get the value like this:

$chkBox = $_POST['chkBox'];

when I loop through the elements:

for ($i=0;$i<=5;$i++) {echo "chkBox value=".$chkBox[$i]."<br>";}

I find that the order of the checked elements is not preserved.  If ANY
2 
chkboxes are checked - the first 2 elements of the array are 'on'.  If
the 
last 3 are checked- the first 3 array elements are 'on'.  If the last cb

is checked, the first array element is 'on', etc...

I sure would like to be able to use this to be able to see WHICH 
checkboxes were checked- right now all this is good for is telling me
how 
many are checked.  

Is there a way to do this?


  Return to Index