0

Here is my form: http://jsfiddle.net/sxGtM/4743/ ... when you submit this form I need create JSON like this

data = {
        'voted_questions': [question.pk, question.pk, question.pk, question.pk],
        'answers': [
        {
        'question': question.pk,
        'option': option.pk,
        },
        {
        'question': question.pk,
        'option': option.pk,
        },
        ]
    }

question.pk and option.pk are numbers which identifying each question and option (it is ID). Could you help me how can I create this JSON?

user3357400
  • 397
  • 2
  • 9
  • 26
  • possible duplicate of [form serialize javascript (no framework)](http://stackoverflow.com/questions/11661187/form-serialize-javascript-no-framework) – Hüseyin BABAL Feb 27 '14 at 19:52

1 Answers1

0

I'm not 100% sure why you want a list of answered questions, when you also have a list of answers (which includes the question IDs). It's what you asked for though, so here you go.

HTML

<h2>Form</h2>
<form action="" method="post">
Which city is in Great Britain?<br/>
<input type="hidden" name="first" value="1"/>
London:<input type="radio" name="first" data-questionid="1" value="11"/><br/>
New York:<input type="radio" name="first" data-questionid="1" value="12"/><br/>
Which city is in USA?<br/>
<input type="hidden" name="second" value="2"/>
Washington:<input type="radio" name="second" data-questionid="2" value="13"/><br/>
Tokio:<input type="radio" name="second" data-questionid="2" value="14"/><br/>
<p><input type="submit" /></p>
</form>
<h2>JSON</h2>
<pre id="result">
</pre>

Javascript

$(function() {
    $('form').submit(function() {
        var voted_questions = [];
        var answers = [];

        $('input[type="radio"]:checked').each(function(){
            voted_questions.push($(this).data('questionid'));
            answers.push({'question':$(this).data('questionid'), 'options':this.value});
        });

        var data = {
            'voted_questions': voted_questions,
            'answers': answers
        };

        $('#result').text(JSON.stringify(data));
        return false;
    });


});

jsfiddle

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
  • thank you, it works ... I add some things here: http://jsfiddle.net/B9r22/2/ there is also type: checkbox, text and hidden ... could you write code for them? three hidden inputs I would like to add to json like this: 'voter': {'ip_address': a.b.c.d, 'voting_started':voting_started, 'voting_ended': voting_ended,}, – user3357400 Feb 27 '14 at 21:53
  • @user3357400 If the code I provided meets your original request, please mark this as the correct/accepted answer. As for your additional request, SO is not a place to ask people to write code for you. What I've provided should be enough for you to figure out how to apply it to additional inputs. At the very least, you need to make an attempt. If you get stuck on something specific, then start a new question, show you code and any related errors, and someone might guide you in the right direction. But never say "I need XYZ, give me code that does this". – Patrick Q Feb 27 '14 at 22:16
  • @user1505169 I code something new here: http://www.jsfiddle.net/B9r22/5/ can you chceck it? how to delete [ ] from voter? I would like to have json like this: 'voter': {'ip_address': a.b.c.d, 'voting_started':voting_started, 'voting_ended': voting_ended,} thank you for your help – user3357400 Mar 05 '14 at 10:48