user244394
user244394

Reputation: 13448

Howto create a JSON from form values?

I am trying to create JSOn from the dynamic form fields and values,

When the user submits, i want to display the json feed in #results

I have the following code snippet below:

<form id="myform" class="form-wd">
    ............
</form>
<div id="results"> </div>
</div>
$('#myform-wiz').on('submit', function(ev) {
    //alert($(this).serialize());
    var data = $(this).serialize(); // -> The URL encoded form data
    $("#results").text(data);
    ev.preventDefault();
});

Upvotes: 0

Views: 112

Answers (2)

woodykiddy
woodykiddy

Reputation: 6455

Alternatively, try out jquery-json plugin and call .toJSON() method to serialize a javascript object, number, string, or array into JSON.

var thing = {plugin: 'jquery-json', version: 2.3};

var encoded = $.toJSON( thing );
// '{"plugin":"jquery-json","version":2.3}'
var name = $.evalJSON( encoded ).plugin;
// "jquery-json"
var version = $.evalJSON(encoded).version;
// 2.3

Upvotes: 0

user1305989
user1305989

Reputation: 3361

I think you are interested in JSON.stringify ?

    data = $(this).serialize();
    json_data = JSON.stringify(data);
     $("#results").text(json_data);  

Upvotes: 2

Related Questions