blurredvision Posted June 9, 2008 Share Posted June 9, 2008 I have a group of checkboxes that are designed to be a "Check All That Apply" type of deal, and upon submission of the form, I want all the boxes that are checked to be entered into an array for me to process. I've briefly googled a solution, but I can't get it to work. I see examples where people have the following setup: <form action=*** method=post> <input type=checkbox name=fruit[] value=apple /> Apple <input type=checkbox name=fruit[] value=orange /> Orange <input type=checkbox name=fruit[] value=banana /> Banana </form> Question 1: Do you have to use the [] in the name for the checkbox group? Question 2: How do I properly check for the existance of this array in PHP so as to run an IF statement? Question 3: What is the proper syntax to assign an array submitted through POST to a variable? I didn't bring my usual PHP books to work with me today, so sorry to ask such a simple question . Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/ Share on other sites More sharing options...
MadTechie Posted June 9, 2008 Share Posted June 9, 2008 <form action='***' method='post'> <input type='checkbox' name='fruit[]' value='apple' /> Apple <input type='checkbox' name='fruit[]' value='orange' /> Orange <input type='checkbox' name='fruit[]' value='banana' /> Banana </form> Question 1: Do you have to use the [] in the name for the checkbox group? Question 2: How do I properly check for the existance of this array in PHP so as to run an IF statement? Question 3: What is the proper syntax to assign an array submitted through POST to a variable? 1.when the form is posted, your get the data like so print_r($_POST['fruit']) this will only show the checked boxes 2. <?php foreach($_POST['fruit'] as $F) { if($F = 'orange') echo "orange Found"; } ?> 3. The array is a Type of variable.! ??? but i think you mean how do i pull it out.. $_POST['fruit'] would be an array ie array[0] = apples array[1] = orange and $_POST['fruit'][0] would be apples No Book needed, just see php.net (best manual on the web) Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/#findComment-561243 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 1) the [] in fruit[] is what signifies it as an array, so yes, you need them 2) you check for them the same way as you check any other posted variable, exept that since it's an array, it now becomes a 2d array, so it would look something like this : // assuming they are all checked echo $_POST['fruit'][0]; // echoes apple echo $_POST['fruit'][1]; // echoes orange echo $_POST['fruit'][2]; // echoes banana Now here's the thing: for example, if you check apple and banana but not orange, then your posted array will only have 2 positions in it, not 3 (with the middle one 'empty') so it will now look like this: echo $_POST['fruit'][0]; // echoes apple echo $_POST['fruit'][1]; // echoes banana If for some reason you need to know that Banana was the 3rd checkbox, you can solve that by specifically mentioning the array position in your form like so: <form action=*** method=post> <input type=checkbox name='fruit[0]' value='apple' /> Apple <input type=checkbox name='fruit[1]' value='orange' /> Orange <input type=checkbox name='fruit[2]' value='banana' /> Banana </form> If you do that, then the posted array will look like this: // assuming they are all checked echo $_POST['fruit'][0]; // echoes apple echo $_POST['fruit'][1]; // echoes nothing, it's empty echo $_POST['fruit'][2]; // echoes banana 3) I think that was probably answered somewhere in 1 and 2 edit: seems like madtechie beat me to the punch. Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/#findComment-561248 Share on other sites More sharing options...
blurredvision Posted June 9, 2008 Author Share Posted June 9, 2008 OK, thanks guys, that helps me to understand some, but let me try to show a bit more detail into what I'm having problems with. I have 5 checkboxes. The first 3 are given a name of 'random[]'. The last two are given a different name, and will not be a part of the random[] array. <form blah blah method=post> <input type=checkbox name=random[] value=apple /> Apple <input type=checkbox name=random[] value=orange /> Orange <input type=checkbox name=random[] value=banana /> Banana <input type=checkbox name=all value=fruits /> All Fruits <input type=checkbox name=all value=vegatables /> All Vegetables </form> The idea is that a user can choose 'All Fruits', 'All Vegetables', or a combination of the choices. I will create the arrays for 'All Fruits' and 'All Vegetables' in my code, but the 'random[]' array will be created by the checkboxes. I have an IF/ELSEIF/ELSE to assing one of these arrays to a single variable: if ($_POST['all'] == 'fruits') { $foodarray = array ('apple', 'orange', 'banana'); } elseif ($_POST['all'] == 'vegetables') { $foodarray = array ('tomato', 'potato', 'cucumber'); } else { $foodarray = $_POST['random']; } I need to assign whatever is chosen to the $foodarray variable because later on in my code, it will check to see $foodarray even exists, and if not, it will use something else. The part I'm having problems with is the assignment of the random[] array to $foodarray. I can print_r and see that the random[] array exists and is getting the correct values, but I don't know the syntax in assigning the random[] array to the $foodarray variable. Thanks for any further help. Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/#findComment-561367 Share on other sites More sharing options...
thebadbad Posted June 9, 2008 Share Posted June 9, 2008 Simple, you already did? $foodarray = $_POST['random']; Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/#findComment-561374 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 I think I personally would suggest sticking to the original principle for the first 3, and for the last 2, since it's really only a matter of convenience for the user, use some javascript to automatically check/uncheck all of them on the form level, before the user submits. That way, you don't have to mess with all that. Because the way you are trying to do it now, you're going to end up having to submit all options regardless, like with a hidden field or something. Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/#findComment-561378 Share on other sites More sharing options...
blurredvision Posted June 9, 2008 Author Share Posted June 9, 2008 Simple, you already did? $foodarray = $_POST['random']; Well, if that's how it's done, that's not working for me for some reason . I also considered throwing some javascript in there, we'll see what I end up doing. This is just a side project at work, so I'm in no rush to get everything working. Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/#findComment-561480 Share on other sites More sharing options...
thebadbad Posted June 9, 2008 Share Posted June 9, 2008 It's easy to check that $_POST['random'] contains what you are expecting. There is no difference in assigning an array to a variable, than assigning a variable ($_POST['random']) containing an array to a variable. Quote Link to comment https://forums.phpfreaks.com/topic/109416-how-to-assign-checked-checkboxes-to-an-array-upon-submission/#findComment-561518 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.