solarisuser Posted March 8, 2006 Share Posted March 8, 2006 I have five checkboxes that I'd like to put in an array only if they have been checked. I'm not sure exactly how to do that.PHP Code:[...snip...]<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field1">Field1BR><INPUT TYPE=CHECKBOX VALUE="field2" NAME="field2">Field2BR><INPUT TYPE=CHECKBOX VALUE="field3" NAME="field3">Field3BR><INPUT TYPE=CHECKBOX VALUE="field4" NAME="field4">Field4BR><INPUT TYPE=CHECKBOX VALUE="field5" NAME="field5">Field5BR>[...snip...]If I have field2, field3, and field5 checked, I'd like to put them in an array, and then use implode() to print them out later. Quote Link to comment Share on other sites More sharing options...
XenoPhage Posted March 8, 2006 Share Posted March 8, 2006 [!--quoteo(post=352704:date=Mar 7 2006, 08:25 PM:name=solarisuser)--][div class=\'quotetop\']QUOTE(solarisuser @ Mar 7 2006, 08:25 PM) [snapback]352704[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have five checkboxes that I'd like to put in an array only if they have been checked. I'm not sure exactly how to do that.PHP Code:[...snip...]<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field1">Field1BR><INPUT TYPE=CHECKBOX VALUE="field2" NAME="field2">Field2BR><INPUT TYPE=CHECKBOX VALUE="field3" NAME="field3">Field3BR><INPUT TYPE=CHECKBOX VALUE="field4" NAME="field4">Field4BR><INPUT TYPE=CHECKBOX VALUE="field5" NAME="field5">Field5BR>[...snip...]If I have field2, field3, and field5 checked, I'd like to put them in an array, and then use implode() to print them out later.[/quote]$myarray = Array($field2, $field3, $field5);? Or maybe this :[code] $myarray = Array(); if (isset($_REQUEST['field1'])) { array_push($myarray, $_REQUEST['field1']); }[/code]And just repeat the if for each field you need in there.. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 8, 2006 Share Posted March 8, 2006 Only those checkboxes that have been checked are actually passed to your processing script. If you change your form to:[code]<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field[]">Field1BR><INPUT TYPE=CHECKBOX VALUE="field2" NAME="field[]">Field2BR><INPUT TYPE=CHECKBOX VALUE="field3" NAME="field[]">Field3BR><INPUT TYPE=CHECKBOX VALUE="field4" NAME="field[]">Field4BR><INPUT TYPE=CHECKBOX VALUE="field5" NAME="field[]">Field5BR>[/code]Only those checked will be in the array $_POST['field'], you can either leave them there or move them to another array. You can see what's in the array by[code]<?php if (isset($_POST['field'])) echo '<pre>' . print_r($_POST['field']) . '</pre>'; ?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
XenoPhage Posted March 8, 2006 Share Posted March 8, 2006 [!--quoteo(post=352864:date=Mar 8 2006, 09:43 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 8 2006, 09:43 AM) [snapback]352864[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]<INPUT TYPE=CHECKBOX VALUE="field1" NAME="field[]">Field1BR><INPUT TYPE=CHECKBOX VALUE="field2" NAME="field[]">Field2BR>[/code]Only those checked will be in the array $_POST['field'], you can either leave them there or move them to [/quote]Oh, neat! It never even dawned on me to try that.. I use that technique with select multiple's all the time.. Thanks for the info! :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.