By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

Processing multiple checkboxes with same name.

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

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

P: n/a

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

P: n/a
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

P: n/a

<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

P: n/a
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

P: n/a
isset () is the best one, but you can use @in_array() to prevent errors
also.

Jul 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.