Jump to content

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/4394-implode-with-array-and-mysql-result/
Share on other sites

[!--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..
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
[!--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! :)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.