3

I've created a form with multiple fields with the same name:

<form ... >
<input type=text name="problem"  value="problem1" />
<input type=text name="question" value="question1" />
<input type=text name="answer"   value="answer1" />

<input type=text name="problem"  value="problem2" />
<input type=text name="question" value="question1" />
<input type=text name="answer"   value="answer1" />
<input type=text name="question" value="question2" />
<input type=text name="answer"   value="answer1" />
<input type=text name="answer"   value="answer2" />
...
<input type=hidden name="jsonString" value="" />

<input type=submit value="Send" />
</form>

those fields can be dynamically added/removed by javascript

I would like to put into hidden field a string (using JSON.strinfigy) and pass this through form before submitting:

{
    problem: [  
        {   
            name: problem 1, 
            question: [
                {
                    name: question 1,
                    answer: [
                        name:answer1
                    ]
                }
            ]
        },

        {   
            name: problem 2, 
            question: [
                {
                    name: question 1,
                    answer: [
                        name:answer1
                    ]
                },  
                {
                    name: question 2,
                    answer: [
                        name:answer1,
                        name:answer2
                    ]
                }   
            ]
        }       
    ]
}

Any ideas to do that? Thanks

Rob

Gore
  • 199
  • 1
  • 2
  • 12

1 Answers1

0

there isn't any way to serialize json from this; Try to create simply, like this:

var myJson;
$("input[name=problem]").each(function(){
   myJson.Push( { "name": $this.val()});
});

(look at adding-removing-items-from-json-data-with-jquery form more instruction)

Community
  • 1
  • 1
mhdrad
  • 330
  • 3
  • 15
  • it seems a good idea... but I've some problems to get the fields: http://jsfiddle.net/sxGtM/2499/ – Gore Oct 15 '12 at 10:30
  • this is **jquery code**, you should write the javascript code in `$(document).ready(function(){ /*codes here*/ }` look at [this fiddle](http://jsfiddle.net/sxGtM/2502/) – mhdrad Oct 15 '12 at 19:57