Store Javascript values in an array

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?

Upvotes: 0

Views: 260

Answers (3)

strike_noir
strike_noir

Reputation: 4174

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

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69905

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(',');

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

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

Upvotes: 1

Related Questions