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 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
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
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-
<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
isset () is the best one, but you can use @in_array() to prevent errors
also. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |