3

I have 100 inputs with the name table[]. How can I get their value with jQuery, as an array?

I am trying to do something like $_POST['table'] in PHP.

I tried the following code, but I want the values as an array...

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

4 Answers 4

5
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 ,.

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

3 Comments

document.write(arrInputValues) outputs test1,,,,,,,,,,,,,,,,,,,,,,,,,,test2 and so on. Is this normal?
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?
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.
3

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

1 Comment

Can You tell me how to get the value for all elements as array? Thank you!
2

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

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

3 Comments

As I post above OK,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 )
@Angelov: It is already an array in $_POST['table']; you can see the contents of that like print_r($_POST['table']);
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
2

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

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

1 Comment

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

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.