views:

74

answers:

4

I have 100 inputs with name - table[]. How to get their value with jQuery,like $_POST['table'] ,as I do in PHP. I tryed Code:

$("input[name='table[]']").each(function()
 {  document.write($(this).val());           });

but I want to get them as an array.

+1  A: 

the [ and ] characters are special characters, you need to escape them

$("input[name='table\\[\\]']").each(function()
 {  document.write($(this).val());           });
Pharabus
OK,but how to get it as array?Now it outputs me that:test1test2test3test4........How to get is likeArray ([0] => test1 [1] => test2 [2] => test3 [3] => test4)
A.Angelov
+1  A: 

You need to escape the [] chars, try this:

$("input[name='table\\[\\]']").each(function()
 ...........
Sarfraz
As I post aboveOK,but how to get it as array? Now it outputs me that: test1test2test3test4........ How to get is like Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 )
A.Angelov
@Angelov: It is already an array in $_POST['table']; you can see the contents of that like print_r($_POST['table']);
Sarfraz
I didn't explain it right.The ide of this all is that I want to transfer the value of the input to another file ,using AJAX,where I will submit that value in BD ,but I must use foreach ,which,I think expects array.foreach($_POST['table'] AS $data) {//THE INSERT TO THE DB}I want to insert it without refreshing the page,while clicking submit ,thats why I use AJAX.But to happen the submission to the DB I must sent the data to the another file ,using AJAX.Thats why I want to make the array in JS.After this I will serialize it to a string and do what I want
A.Angelov
+3  A: 

Following code gives the value of each input

 var arr=new Array();
 $("input[name='table[]']").each(function()
 {  
    var val=$(this).attr('value');
    document.write(val);
    arr.Push(val);
 });
Kushal Waikar
Can You tell me how to get the value for all elements as array?Thank you!
A.Angelov
Please refer to updated answer. Thanks !
Kushal Waikar
+1  A: 
var arrInputValues = new Array();
$("input[name='table\\[\\]']").each(function(){
     arrInputValues.push($(this).val()); 
     // you can also use this
     //arrInputValues.push(this.value);
});

Now your arrInputValues contains all the value.

You can also use

$("input[name='table[]']").each(function(){

You can see a working demo

You can use join() method to join the values in the array.

arrInputValues.join(',');

will join the array elements as a string separated by ,.

rahul
document.write(arrInputValues) outputs test1,,,,,,,,,,,,,,,,,,,,,,,,,,test2 and so on.Is this normal?
A.Angelov
Yes, it is normal.
rahul
Thank you!And how to unjoin them?Will it work if I process the string through foreach in PHP and insert it in DB.Do You see my answer below?
A.Angelov
You can split the string in php using the `,` and get the values. But you have to be careful when choosing the delimiter. Any of the values should not contain the delimiter character.
rahul
Thank you very much!You are amazing!http://dagobah.biz/flash/thank_you.swf
A.Angelov