开发者

Store Javascript values in an array

I have this javascript code:

var allVals = [];
$('#c_b :checked').each(function() {
   allV开发者_开发问答als.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?


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜