473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Checkboxs and array indices

I am having a problem with checkboxes...

I have a number of blocks like this on my form:

<input type="text" name="name[]" value="a name">
<input type="text" name="ddi[]" value="1234">
<input type="checkbox" name="delete[]">

If I then submit the form, the array $delete is the only one which isn't
indexed by its order on the form.

For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the first
name[] and ddi[] input on the form.

If I check any one of the checkboxes, say the third one, then $delete[ 0 ]
will be "on", and all of the other including $delete[ 2 ] will be off.

If I checked the third and seventh checkbox, just as an example, $delete[
0 ] and $delete[ 1 ] will be "on".

So, there is no way I can tell which checkboxes are actually checked, just
how many have been checked. This is extremely annoying and doesn't happen
with any of the other form controls. Any ideas?

--
"Come to think of it, there are already a million monkeys on a million
typewriters, and the Usenet is NOTHING like Shakespeare!" - Blair Houghton
-=-=-=-=-=-=-=-=-=-=-=-
http://www.nrkn.com/
-=-=-=-=-=-=-=-=-=-=-=-
Jul 17 '05 #1
3 1694
Nik Coughin wrote:
I am having a problem with checkboxes...

I have a number of blocks like this on my form:

<input type="text" name="name[]" value="a name">
<input type="text" name="ddi[]" value="1234">
<input type="checkbox" name="delete[]">

If I then submit the form, the array $delete is the only one which
isn't indexed by its order on the form.

For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the
first name[] and ddi[] input on the form.

If I check any one of the checkboxes, say the third one, then
$delete[ 0 ] will be "on", and all of the other including $delete[ 2
] will be off.
If I checked the third and seventh checkbox, just as an example,
$delete[ 0 ] and $delete[ 1 ] will be "on".

So, there is no way I can tell which checkboxes are actually checked,
just how many have been checked. This is extremely annoying and
doesn't happen with any of the other form controls. Any ideas?


Figured it out. You have to give the checkboxes a value and check for the
value. It's not so simple as checking if a given checkbox returns "on".
Though why on Earth it does what I've described above when you omit the
value is beyond me.
Jul 17 '05 #2
Nik Coughin wrote:
I am having a problem with checkboxes...

I have a number of blocks like this on my form:

<input type="text" name="name[]" value="a name">
<input type="text" name="ddi[]" value="1234">
<input type="checkbox" name="delete[]">

If I then submit the form, the array $delete is the only one which
isn't indexed by its order on the form.

For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the
first name[] and ddi[] input on the form.

If I check any one of the checkboxes, say the third one, then $delete[
0 ] will be "on", and all of the other including $delete[ 2 ] will be
off.

If I checked the third and seventh checkbox, just as an example,
$delete[ 0 ] and $delete[ 1 ] will be "on".

So, there is no way I can tell which checkboxes are actually checked,
just
how many have been checked. This is extremely annoying and doesn't
happen
with any of the other form controls. Any ideas?


It's because checkboxes aren't submitted by the browser if there aren't
checked. PHP has no way of knowing there were eg 5 checkboxes on the
form all named delete[] and that only 2 were submitted; it just sees
the two that were submitted.

Another way to do it is like so, where you explicitly name the
checkboxes with an incrementing number:

<input type="checkbox" name="delete[0]">
<input type="checkbox" name="delete[1]">
<input type="checkbox" name="delete[2]">

This should then give you the behaviour you are expecting.

In the example above, if you checked the first and last ones, using
print_r($_POST) you would get the following:

[delete] => Array
(
[0] => on
[2] => on
)

Note that [1] is not set because it was not posted.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #3
Nik Coughin wrote:
Nik Coughin wrote:
I am having a problem with checkboxes...

I have a number of blocks like this on my form:

<input type="text" name="name[]" value="a name">
<input type="text" name="ddi[]" value="1234">
<input type="checkbox" name="delete[]">

If I then submit the form, the array $delete is the only one which
isn't indexed by its order on the form.

For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the
first name[] and ddi[] input on the form.

If I check any one of the checkboxes, say the third one, then
$delete[ 0 ] will be "on", and all of the other including $delete[ 2
] will be off.
If I checked the third and seventh checkbox, just as an example,
$delete[ 0 ] and $delete[ 1 ] will be "on".

So, there is no way I can tell which checkboxes are actually checked,
just how many have been checked. This is extremely annoying and
doesn't happen with any of the other form controls. Any ideas?


Figured it out. You have to give the checkboxes a value and check for
the
value. It's not so simple as checking if a given checkbox returns
"on". Though why on Earth it does what I've described above when you
omit the value is beyond me.


Have a read my other post. The reason it does it the way is does, is
because the data is not sent from the browser in the first place if the
checkbox is not checked.

Consider the following when you have actually assigned a value to the
checkbox:

Select your favourite activites from the following:
<input type="checkbox" name="foo[]" value="swimming">Swimming
<input type="checkbox" name="foo[]" value="cycling">Cycling
<input type="checkbox" name="foo[]" value="running">Running
<input type="checkbox" name="foo[]" value="skiing">Skiing

The user is supposed to select zero or more options from the list. If
the user only checked two of the four options do you think it should
still submit all the options? If it did, how would your server sided
script know which was checked?

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
9
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
7
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
9
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array...
11
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.