473,765 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1713
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
3136
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 items from the original order cleaned up in the array. What I've tried to do so far is: 1) The data is stored in a serialized array in the order_data field in the orders table. When the order is selected, it is unserialized and called $order_data....
6
2745
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 (i.e., a matrix) it seems to be trivial, with array indexing, to extract a subset of its *columns*. But it does not seem to be trivial to extract a subset of its *rows*. The code snippet below describes the problem (if it really is a problem)...
11
3267
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? A.sort().slice(0,n) // would do it, but sorts; and the number
9
3441
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 I'm just asking for a few pointers (or the full solution if you have the time) for what I want to do. Basically, I want to write a javascript wherby I only need to pass it the names of form fields - then my javascript will check each form field...
7
2378
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 location of them in the array /list. Hence the output being some list of lists, containing groups of indices of the storage array that point to the same node index. This is obviously a trivial problem, but if my storage list is large and the set of...
22
4642
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 month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
29
5475
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 elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
9
3750
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 with dimensional lengths of a, b, c and d. Thanx for any help.
11
4674
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 cli:array<cli::interior_ptr<float>, 2>... So, what do I do?
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.