0

If I have an html form where the name values are:

name="main[name]"
name="main[email]"
name="main[info]"

How would I go about getting all the values of the array in the form of:

main : { name : 'value', email : 'value', info : 'value' }

I've tried jquery's serialize() from an older stack (Obtain form input fields using jQuery?) question but that solution gives me an array along the lines of

{ 'main[email]' : 'value', 'main[email]' : 'value', 'main[info]' : 'value' }

EDIT: reason i'm doing the main[email] format is because I have an extra input there for csrf which I need to differentiate from the actual input values, the final array is gonna be something like:

  [ csrf: '', main : { name : 'value', email : 'value', info : 'value' } ]

this way I can just use main values.

Community
  • 1
  • 1
cat-t
  • 1,346
  • 1
  • 18
  • 34
  • Hehe, and people called me crazy for rejecting jQuery and writing my own code... Who's laughing now, when I have beautifully serialized forms that include the button you clicked and even the coordinates of an image input! :3 – Niet the Dark Absol Feb 11 '14 at 20:30
  • If you are looking to pass along arrays as values, you will have to manually create the array and drop them into the value on the post – VIDesignz Feb 11 '14 at 20:39
  • 1
    Have a look at this http://stackoverflow.com/a/8407771/597419 – Danny Feb 11 '14 at 20:41
  • 1
    @NiettheDarkAbsol While I am not fanboy of jQuery, that functionality could just as easily be created using jQuery. No one calls anyone crazy for not using a framework -- when it is appropriate to not use a framework. – Chris Baker Feb 11 '14 at 20:59

1 Answers1

2

Why not just set your fields like this?

 name="name"
 name="email"
 name="info"
VIDesignz
  • 4,703
  • 3
  • 25
  • 37
  • looking for a way to loop all the data into an array – cat-t Feb 11 '14 at 20:40
  • 1
    @cat-t If you named your fields as suggested, `serialize()` would give you an "array" with the data and appropriate keys (actually, an object that you can treat like an associative array) – Chris Baker Feb 11 '14 at 21:00