Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize form data to JSON [duplicate]

I want to do some pre-server-validation of a form in a Backbone.js model. To do this I need to get the user input from a form into usable data. I found three methods to do this:

  1. var input = $("#inputId").val();
  2. var input = $("form.login").serialize();
  3. var input = $("form.login").serializeArray();

Unfortunately, none of the provide a good reabable and developable JSON object which I require. I already looked through several questions on Stack Overflow, but I found only some extra libraries.

Doesn't Underscore.js, the current jQuery or Backbone.js provide a helper method?

I can't imagine there is no request for such a function.

HTML

<form class="login">     <label for="_user_name">username:</label>     <input type="text" id="_user_name" name="user[name]" value="dev.pus" />     <label for="_user_pass">password:</label>     <input type="password" id="_user_pass" name="user[pass]" value="1234" />     <button type="submit">login</button> </form> 

JavaScript

var formData = $("form.login").serializeObject(); console.log(formData); 

Outputs

{     "name": "dev.pus",     "pass": "1234" } 

Backbone.js model

var user = new User(formData); user.save(); 
like image 857
dev.pus Avatar asked Jul 05 '12 06:07

dev.pus


People also ask

How can I convert form data to JSON?

How to Convert Form Data to JSON With Object. fromEntries() Note: For both methods, we can use JSON. stringify() to convert the object into a JSON string, which we can then use to send JSON encoded data to APIs.

How do I save HTML form data to JSON file?

stringify($("#emails_form"). serializeArray()); If you want to store formData in a JSON file, you need to post it to the server (e.g. per AJAX) and save it. But in that case, you can simply post the form und convert it to JSON on the server itself.

How do you serialize form data?

jQuery serialize() MethodThe serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

How do you serialize form data with vanilla JS?

To serialize a FormData object into a query string, pass it into the new URLSearchParams() constructor. This will create a URLSearchParams object of encoded query string values. Then, call the URLSearchParams. toString() method on it to convert it into a query string.


2 Answers

Here's a function for this use case:

function getFormData($form){     var unindexed_array = $form.serializeArray();     var indexed_array = {};      $.map(unindexed_array, function(n, i){         indexed_array[n['name']] = n['value'];     });      return indexed_array; } 

Usage:

var $form = $("#form_data"); var data = getFormData($form); 
like image 176
Maciej Pyszyński Avatar answered Sep 23 '22 07:09

Maciej Pyszyński


You can do this:

function onSubmit( form ){    var data = JSON.stringify( $(form).serializeArray() ); //  <-----------      console.log( data );    return false; //don't submit  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <form onsubmit='return onSubmit(this)'>    <input name='user' placeholder='user'><br>    <input name='password' type='password' placeholder='password'><br>    <button type='submit'>Try</button>  </form>

see this: http://www.json.org/js.html

like image 30
Mohammad Adil Avatar answered Sep 23 '22 07:09

Mohammad Adil