473,221 Members | 1,722 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,221 software developers and data experts.

Processing multiple checkboxes with same name.

Hi,

I have multiple checkboxes on the same form as follows:

<input type='checkbox' name='cbtype[]' value='1'>
<input type='checkbox' name='cbtype[]' value='2'>
<input type='checkbox' name='cbtype[]' value='3'>

I'm wanting to re-display the same checkboxes, but if the user
previously checked one of them then I want it automatically checked on
the subsequent display. At the moment I'm doing this:

$checked = '';
if (in_array('1',$HTTP_POST_VARS[cbtype])) { $checked = ' checked'; }
<input type='checkbox' name='cbtype[]' value='1' $checked>

This works perfectly unless the user hasn't selected any of the
checkboxes in which case I get the following error thrown at me:

Warning: in_array(): Wrong datatype for second argument in...

Obviously PHP isn't creating an array for the checkbox selection data
unless it has to, and so "in_array" is throwing a wobbly.

Presumably there's a better way to do this?

TIA

Jul 14 '06 #1
6 14724

je******@gmail.com wrote:
Hi,

I have multiple checkboxes on the same form as follows:

<input type='checkbox' name='cbtype[]' value='1'>
<input type='checkbox' name='cbtype[]' value='2'>
<input type='checkbox' name='cbtype[]' value='3'>

I'm wanting to re-display the same checkboxes, but if the user
previously checked one of them then I want it automatically checked on
the subsequent display. At the moment I'm doing this:

$checked = '';
if (in_array('1',$HTTP_POST_VARS[cbtype])) { $checked = ' checked'; }
<input type='checkbox' name='cbtype[]' value='1' $checked>

This works perfectly unless the user hasn't selected any of the
checkboxes in which case I get the following error thrown at me:

Warning: in_array(): Wrong datatype for second argument in...

Obviously PHP isn't creating an array for the checkbox selection data
unless it has to, and so "in_array" is throwing a wobbly.

Presumably there's a better way to do this?

TIA
Hiya

Use isset to check cbtype is in the post vars to avoid the warning

Another way is echo + check the checkboxes in a loop....The more
checkboxes to show the more useful it is to loop

$checkboxLabels = ( 1 ='Label1', 'Label2', 'Label3' );
$cbtype_set = isset($_POST['cbtype']);

foreach( $checkboxLabels as $id =$label ) {
$checked = ( $cbtype_set && in_array($id, $_POST['cbtype']) ) ? '
checked="checked"' : '';

echo '<input type="checkbox" name="cbtype[]" value="', $id , '",
$checked, '>',$label;
}

Tim

Jul 14 '06 #2

A quick fix would be to check that $_REQUEST['cbtype'] exists or is an
array before continuing with your html generation.
je******@gmail.com wrote:
Hi,

I have multiple checkboxes on the same form as follows:

<input type='checkbox' name='cbtype[]' value='1'>
<input type='checkbox' name='cbtype[]' value='2'>
<input type='checkbox' name='cbtype[]' value='3'>

I'm wanting to re-display the same checkboxes, but if the user
previously checked one of them then I want it automatically checked on
the subsequent display. At the moment I'm doing this:

$checked = '';
if (in_array('1',$HTTP_POST_VARS[cbtype])) { $checked = ' checked'; }
<input type='checkbox' name='cbtype[]' value='1' $checked>

This works perfectly unless the user hasn't selected any of the
checkboxes in which case I get the following error thrown at me:

Warning: in_array(): Wrong datatype for second argument in...

Obviously PHP isn't creating an array for the checkbox selection data
unless it has to, and so "in_array" is throwing a wobbly.

Presumably there's a better way to do this?

TIA
Jul 14 '06 #3
je******@gmail.com wrote:
Hi,

I have multiple checkboxes on the same form as follows:

<input type='checkbox' name='cbtype[]' value='1'>
<input type='checkbox' name='cbtype[]' value='2'>
<input type='checkbox' name='cbtype[]' value='3'>

I'm wanting to re-display the same checkboxes, but if the user
previously checked one of them then I want it automatically checked on
the subsequent display. At the moment I'm doing this:

$checked = '';
if (in_array('1',$HTTP_POST_VARS[cbtype])) { $checked = ' checked'; }
<input type='checkbox' name='cbtype[]' value='1' $checked>

This works perfectly unless the user hasn't selected any of the
checkboxes in which case I get the following error thrown at me:

Warning: in_array(): Wrong datatype for second argument in...

Obviously PHP isn't creating an array for the checkbox selection data
unless it has to, and so "in_array" is throwing a wobbly.

Presumably there's a better way to do this?

TIA
I usually unroll the checkbox as follows:

$boxes = array(1, 2, 3);
foreach( $boxes as $box ) {
$checked = isset($_POST['cbtype'][$box]) ? 'checked' : '';
printf("<input type=\"checkbox\" name=\"cbtype[]\" value=\"%d\" %s>\n",
$box, $checked);
}

-david-

Jul 14 '06 #4

<je******@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
Hi,

I have multiple checkboxes on the same form as follows:

<input type='checkbox' name='cbtype[]' value='1'>
<input type='checkbox' name='cbtype[]' value='2'>
<input type='checkbox' name='cbtype[]' value='3'>

I'm wanting to re-display the same checkboxes, but if the user
previously checked one of them then I want it automatically checked on
the subsequent display. At the moment I'm doing this:

$checked = '';
if (in_array('1',$HTTP_POST_VARS[cbtype])) { $checked = ' checked'; }
<input type='checkbox' name='cbtype[]' value='1' $checked>

This works perfectly unless the user hasn't selected any of the
checkboxes in which case I get the following error thrown at me:

Warning: in_array(): Wrong datatype for second argument in...

Obviously PHP isn't creating an array for the checkbox selection data
unless it has to, and so "in_array" is throwing a wobbly.

Presumably there's a better way to do this?

TIA
Why not try putting this inside a test using

if (isset($HTTP_POST_VARS[cbtype]) {
blah
}

Shelly
Jul 14 '06 #5
JDS
On Fri, 14 Jul 2006 06:00:39 -0700, je******@gmail.com wrote:
Presumably there's a better way to do this?
And the answer is.....
isset()

Based on the general consensus of four very similar posts that came before
mine.

Yay! I like it when everyone agrees.

--
JDS | je*****@go.away.com
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 15 '06 #6
isset () is the best one, but you can use @in_array() to prevent errors
also.

Jul 15 '06 #7

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

Similar topics

8
by: Ralph Freshour | last post by:
I have multiple checkbox's created with an array name because I have many on the same web page - their names are like: frm_chk_delete frm_chk_delete frm_chk_delete frm_chk_delete etc. Here...
5
by: @(none) | last post by:
I have a page which is a set of CheckBoxes generated daily and thus the number of Checkboxes changes each day. What I want to do is allow the user to select one or more checkboxes and the push a...
17
by: wolfing1 | last post by:
Like, I know if several checkboxes have the same name, I can get the ones that were checked after submit with a request.form("name") and maybe do a split(request.form("name"),",") to cycle through...
0
by: Moskie | last post by:
I have a form where the user is able to provide an arbitrary number of attachments. This is done through Javascript that dynamically generates INPUT fields, each of which has the NAME attribute set...
17
by: Sam Kong | last post by:
Hello, There are 1 or more select elements with the same name in a form. Let's say that the name of the select is 'select1' and the name of the form is 'form1'. If there's only one select I...
6
by: dream2rule | last post by:
Hello All, I am trying to validate multiple checkboxes whose values are stored in an array using php. I have been trying from a really long time but nothing's working out. Can anyone help? ...
0
by: Ned Balzer | last post by:
Hi, Can anyone point me in the direction of a solution for validating multiple checkboxes in an asp.net 2.0 page? 1) This is not a checkboxlist, it is a number of separate checkboxes 2) I do...
3
sunbin
by: sunbin | last post by:
Hi, I am having in a Trouble when working with dynamic checkboxes (i.e. checkboxes with the same name, e.g. <input type="checkbox" name = "check" value="dynamic integer value">) I have...
1
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.