473,406 Members | 2,549 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,406 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 14742

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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,...

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.