Jump to content

Recommended Posts

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 :).

<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)

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.

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.

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.

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.

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.

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.