3

i'm getting the data from a form when it is submitted like this

        values = {};

        $("#myForm").submit(function(){
            $.each($('#myForm').serializeArray(), function(i, field) {
                if(field.name != 'r'){
                    values[field.name] = field.value;

                }
            }); 

            return false;
        });

The problem is that i want to do that multiple times and store all the data in the var values using field.name as a keys and the values as an array to compare it in php i would do values[field.name][] = field.value; is there any similar syntax in js ?

0

1 Answer 1

4

Yeah, you can add multiple values using the Array.push method. But first, you must define values[field.name] as array, like this:

values[field.name] = [];
values[field.name].push(somevalue);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you i tried push before but i didn't know i had to define the array first

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.