0

I have this javascript code:

var allVals = [];
$('#c_b :checked').each(function() {
   allVals.push($(this).val());
});
alert(allVals + "is checked!");

The code above, will list all the checked values from checkboxes like this: value,value,value

How can I, when submitting my form, store those values in a PHP array, so I can use them?

2
  • When you submit a form, if a box is not checked it's value won't be sent. So you really don't need that js code above. Maybe I misunderstood your question? Commented Aug 26, 2011 at 19:55
  • @Brian: That's a non sequitur; the JS code above doesn't do anything about sending anything. Commented Aug 26, 2011 at 19:56

3 Answers 3

1

Name all your checkboxes "c_b[]".

The values of those that are checked will then be accessible in the target PHP script as the array $_POST['c_b'] (or $_GET['c_b']).


This is covered fairly well in the relevant manual page, and the manual's FAQ (oh, the irony!).

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array's join method to get all the array values as a combined string and then pass it along with the form while submitting it as a hidden field. At the Php side you can split this string and then convert it into an array.

var strAllVals = allVals.join(',');

1 Comment

A hilarious way to send "arrays" to a form target!
0

Make all your checkboxes name to 1 name.

Then PHP will have the values in an array.

Btw if you re using jquery AJAX you could use serializeArray()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.