473,836 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14760

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="checke d"' : '';

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******** *************@m 79g2000cwm.goog legroups.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_PO ST_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
2844
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 is my code line that creates each checkbox (the php variable get
5
5897
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 "Done" button and then have a script which uses a "for" loop to check the status of each box. The code I use for this is Sample checkbox HTML <P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox0"
17
16201
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 them. I tried the same thing with textfields (input type=text), but doesn't seem to work the same. Do they behave differently?
0
1286
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 to the same value. Now on the server side processing of this form, how do I handle these posted files, each of which has the same name? I was expecting the Request.Files collection to behave similarly to the Request.Form collection, in that...
17
17104
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 access it like form1.select1. If there are more than one select, I access them like form1.select1. To check if there are more than one, I thought I can use
6
15641
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? Here's the code: <form id="create_user" name="create_user" method="post" action="creating_database.php" onsubmit="return validate_create_user_form();">
0
1643
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 not need to ensure that they are checked, what I am trying to do is lock down the page to prevent possible code injection. So, the checkboxes can be either checked or unchecked, I don't care.
3
2610
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 submitted an HTML form (which contains dynamic textboxes and dynamic checkboxes with a Post Button(input type="button")) by using Ajax function. (onClick="return ajaxFunction(document.getElementById('formid'));") In the ajax function, i have collected...
1
5190
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 checkboxes (code pasted below). But if name of the checkbox array is 'chkbx_ary' then it's failing. I want the name to be 'chkbx_ary' because I want to access this array at server side. Though one may not require to see php part but I'm still pasting...
0
9816
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
9668
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
10840
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
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9371
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...
0
5647
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3112
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.