Merging List of JSONs into one JSON in javascript - javascript

I have an HTML form and I'm trying to stringify and parse its content into JSON on submission using javascript
for example:
<form id="form1" enctype="multipart/form-data">
<input type="hidden" name="presented" value="1" />
<input type="checkbox" name="RESPONSE" value="ch1">ch1</input>
<input type="checkbox" name="RESPONSE" value="ch2">ch2</input>
<input type="checkbox" name="RESPONSE" value="ch3">ch3</input>
<input id="submit_button" name="submit" type="submit"/>
</form>
and the javascript:
$(document).ready(function () {
$("#submit_button").click(function (e) {
e.preventDefault();
var formData = JSON.parse(JSON.stringify(jQuery('#form1').serializeArray()));
alert(JSON.stringify(formData));
});
});
I'm getting an output like this (considering that only the first two checkboxes are checked):
[{"name":"presented","value":"1"},
{"name":"RESPONSE","value":"ch1"},
{"name":"RESPONSE","value":"ch2"}]
but I'm expecting this result in one JSON with arrays for duplicated keys:
{"presented" : "1", "RESPONSE" : ["ch1", "ch2"]}
jsfiddle here

You will need to write your own serializer. For example to group values of the elements with the same name you could do something like this using Array.prototype.reduce:
$("#submit_button").click(function (e) {
e.preventDefault();
var formData = jQuery('#form1').serializeArray().reduce(function(prev, curr) {
if (prev.hasOwnProperty(curr.name)) {
prev[curr.name] = $.isArray(prev[curr.name]) ? prev[curr.name] : [prev[curr.name]]
prev[curr.name].push(curr.value);
}
else {
prev[curr.name] = curr.value;
}
return prev;
}, {});
document.querySelector('pre').innerHTML = JSON.stringify(formData, null, 4);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" enctype="multipart/form-data">
<input type="hidden" name="presented" value="1" />
<input type="checkbox" name="RESPONSE" value="ch1">ch1</input>
<input type="checkbox" name="RESPONSE" value="ch2">ch2</input>
<input type="checkbox" name="RESPONSE" value="ch3">ch3</input>
<input id="submit_button" name="submit" type="submit"/>
</form>
<pre></pre>

I think that your new object's value should always be an array for consistency. Here's how to build it:
var arr = [{"name":"presented","value":"1"},
{"name":"RESPONSE","value":"ch1"},
{"name":"RESPONSE","value":"ch2"}];
var newObj = {};
arr.forEach(function(item){
if(!newObj.hasOwnProperty(item.name)){
newObj[item.name] = [];
}
newObj[item.name].push(item.value);
});
And here's a way to build it exactly like you want it:
var newObj = {};
arr.forEach(function (item) {
if (!newObj.hasOwnProperty(item.name)) {
newObj[item.name] = item.value;
} else {
if (Object.prototype.toString.call(newObj[item.name]) !== '[object Array]') {
var newItem = newObj[item.name];
newObj[item.name] = [newItem];
}
newObj[item.name].push(item.value);
}
});

Think other than JSON parsing library. It's very simple Java Program using String.split() method that convert Json String into name> without using any library.

Related

get checkbox values from form and add to JSON string using JavaScript

attempting to pull form values and put them into localStorage via JSON string. This code works for everything but checkbox values. How do i also get checkbox values? Please and thanks!
<form id="myForm">
<input type="submit" name="submit" value="submitOrder">
</form>
const userOrder = {};
function getValues(e) {
// turn form elements object into an array
var elements = Array.prototype.slice.call(e.target.elements);
// go over the array storing input name & value pairs
elements.forEach((el) => {
if(el.type !== "submit" && el.type !=="button") {
userOrder[el.name] = el.value;
}
});
// finally save to localStorage
localStorage.setItem('userOrder', JSON.stringify(userOrder));
}
document.getElementById("myForm").addEventListener("submit", getValues);
console.log(localStorage.getItem('userOrder'));
use the .checked attribute of a checkbox to tell if it is checked or not
const userOrder = {};
function getValues(e) {
e.preventDefault();
// turn form elements object into an array
//you can also use Array.from(e.target.elements)
var elements = Array.prototype.slice.call(e.target.elements);
console.log(elements);
// go over the array storing input name & value pairs
elements.forEach((el) => {
if(el.type == "checkbox") {
userOrder[el.name] = el.checked;
}
});
console.log(userOrder);
// finally save to localStorage
//localStorage.setItem('userOrder', JSON.stringify(userOrder));
}
document.getElementById("myForm").addEventListener("submit", getValues);
//console.log(localStorage.getItem('userOrder'));
<form id="myForm">
<input type="checkbox" name="checkbox-0">
<input type="checkbox" name="checkbox-1">
<input type="checkbox" name="checkbox-2">
<input type="submit" name="submit" value="submitOrder">
</form>
You can use JQuery serialize() function.
Then, you can do something like this:
function onSubmit( form ){
var data = JSON.stringify( $(form).serializeArray() ); // <-----------
console.log( data );
return false;
}
<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>
<input type='checkbox' name='remember-me'>
<br />
<button type='submit'>Try</button>
</form>

Not able to set the JavaScript JSON object value into Form

My JSP:
<form:form modelAttribute="myForm" action="/action">
<input type="checkbox" value="1" name="checkboxValue" />
<input type="hidden" name="jsonObject" id="jsonObj" value=""/>
<input type="submit" value="Submit" onclick="function getjson()"/>
</form:form>
JavaScript:
function getjson(){
var json = [];
var checkedBoxes = $('input[name="checkedList"]:checked').map(function() {
return this.value;
}).get();
var checkedBoxes1 = $('input[name="checkedList1"]:checked').map(function() {
return this.value;
}).get();
json.push({"id":checkedBoxes});
json.push({"Flap":checkedBoxes1});
document.getElementById("jsonObj").value = json;
}
I have a MyForm.Java:
private String checkboxValue;
private jsonObject jsonObject;
//getters and setters
Here the value of checkboxValue is saving in variable checkboxValue but the jsonObject is not binding with the variable. I don't know why. Need some serious help please.
Your jsp should be like this
<form:form modelAttribute="myForm" action="/action" id="Submitform">
<input type="checkbox" value="1" name="checkboxValue" />
<input type="hidden" name="jsonObject" id="jsonObj" value=""/>
<input type="button" value="Submit" onclick="getjson()"/>
</form:form>
And Your javascript should change to this
function getjson(){
var json = [];
var checkedBoxes = $('input[name="checkedList"]:checked').map(function() {
return this.value;
}).get();
var checkedBoxes1 = $('input[name="checkedList1"]:checked').map(function() {
return this.value;
}).get();
json.push({"id":checkedBoxes});
json.push({"Flap":checkedBoxes1});
document.getElementById("jsonObj").value = json;
document.getElementById("Submitform").submit();
}

How to convert form input fields to nested json structure using jquery

I would like to convert the below input fields into nested json file structure as per below in my html page, I've tried in a couple of ways, but unable to get. Here I am getting these inputs through angularjs ng-repeat. Please help me that how can i implement. Thanks in advance.
<form id="text" method="post" action="" enctype="multipart/form-data">
<input type="hidden" value="/images/Image1.nii" name="imageurl">
<input type="hidden" value="3d0" name="3d" >
<input type="hidden" value="sliceX0" name="sliceX">
<input type="hidden" value="sliceY0" name="sliceY">
<input type="hidden" value="sliceZ0" name="sliceZ">
<input type="hidden" value="/images/Image2.nii" name="imageurl">
<input type="hidden" value="3d1" name="3d1" >
<input type="hidden" value="sliceX1" name="sliceX">
<input type="hidden" value="sliceY1" name="sliceY">
<input type="hidden" value="sliceZ1" name="sliceZ">
</form>
As I am getting it in a normal json structure:
[{"name":"imageurl","value":"/images/Image1.nii"},{"name":"3d","value":"3d0"},{"name":"sliceX","value":"sliceX0"},{"name":"sliceY","value":"sliceY0"},{"name":"sliceZ","value":"sliceZ0"},{"name":"imageurl","value":"/images/Image2.nii"},{"name":"3d","value":"3d1"},{"name":"sliceX","value":"sliceX1"},{"name":"sliceY","value":"sliceY1"},{"name":"sliceZ","value":"sliceZ1"}]
but I need like below:
[
{"name":"imageurl","value":"/images/Image1.nii", parts: [
{"name":"3d","value":"3d0"},
{"name":"sliceX","value":"sliceX0"},
{"name":"sliceY","value":"sliceY0"},
{"name":"sliceZ","value":"sliceZ0"},
]},
{"name":"imageurl","value":"/images/Image2.nii", parts: [
{"name":"3d","value":"3d1"},
{"name":"sliceX","value":"sliceX1"},
{"name":"sliceY","value":"sliceY1"},
{"name":"sliceZ","value":"sliceZ1"}
]}
]
How to convert form input fields to nested json structure using jquery
You should use a helper library like lodash to do such data manipulation.
Here is a conversion which I did with lodash:
// split the data into chunks of 5 elements each
var chunks = _.chunk(data, 5);
// for every chunk create the desired object with the parts
var desired = _.map(chunks, function(c) {
var image = c[0];
image.parts = _.rest(c);
return image;
});
console.log(desired);
Here is a working Plunker
You should wrap each image group in its own container. But if you must keep your current structure:
var el = document.querySelectorAll('#text [type="hidden"]');
var arr = [];
var obj = {};
var push = false;
for(var i = 0; i < el.length; i ++){
if(el[i].value.indexOf('images') > -1){
if(push){
arr.push(obj);
obj = {};
};
obj.name = 'imageurl';
obj.value = el[i].value;
push = true;
}
obj.parts = obj.parts || [];
obj.parts.push({
name: el[i].name,
value: el[i].value
});
}

How to get a key/value data set from a HTML form

I'm simply looking for a way to get all the values from a <form>.
I searched the Web for a while, stumbling across FormData, which seems quite what I'm looking for.
However its API is not available on any browser, so I need an alternative.
What I need in my specific case is an object of key/value pairs. For example:
<form>
<input type="text" name="firstname" value="John" />
<input type="text" name="surname" value="doe" />
<input type="email" name="email" value="" />
<input type="radio" name="gender" value="male" />
<input type="radio" name="gender" value="female" />
</form>
The object should be:
{
firstname: "John",
surname: "doe",
email: "",
gender: ""
}
Edit: The above is just an example, it should work not only with <input> but also with the other tags (e.g. <select>, <textarea> and so on... even <input type="file"> should be supported).
When I originally wrote this answer FormData was not widely supported (and this was called out explicitly in the question). Now that it's been 6 years, FormData has excellent cross-browser support.
Because of this, I highly recommend using FormData directly to access data or serialize data to the server.
Jake Archibald has an excellent post describing FormData (and URLSearchParams) in depth, which I won't attempt to reproduce here, however I will include a few snippets that are relevant:
You can populate FormData state directly:
const formData = new FormData();
formData.set('foo', 'bar');
formData.set('hello', 'world');
...you can read an HTML form directly as FormData:
const formElement = document.querySelector('form');
const formData = new FormData(formElement);
console.log(formData.get('username'));
...you can use FormData directly as a fetch body:
const formData = new FormData();
formData.set('foo', 'bar');
formData.set('hello', 'world');
fetch(url, {
method: 'POST',
body: formData,
});
I recommend reading Jake's post in full and using the APIs that come with the browser over adding more code to build a less-resilient version of the same thing.
My original post preserved for posterity's sake:
Without a strong definition of what should happen with edge cases, and what level of browser support is required, it's difficult to give a single perfect answer to the question.
There are a lot of form behaviors that are easy to miss, which is why I recommend using a well-maintained function from a library, such as jQuery's serializeArray():
$('form').serializeArray();
I understand that there's a big push recently to move away from needlessly including jQuery, so for those who are interested in a vanilla JS solution serializeArray just won't do.
The next difficulty comes from determining what level of browser support is required. HTMLFormElement.elements significantly simplifies a serialization implementation, and selecting the form-associated elements without it is quite a pain.
Consider:
<form id="example">...</form>
<input type="text" form="example" name="lorem" value="ipsum"/>
A conforming implementation needs to include the input element. I will assume that I can use it, and leave polyfilling it as an exercise to the reader.
After that it'd unclear how <input type="file"/> should be supported. I'm not keen on needlessly serializing file elements into a string, so I've made the assumption that the serialization will be of the input's name and value, even though the value is practically useless.
Lastly, an input structure of:
{
'input name': 'value',
'textarea name': 'value'
}
Is excessively naive as it doesn't account for <select multiple> elements, or cases where two inputs have the same name. I've made the assumption that the input would be better as:
[
{
name: 'input name',
value: 'value'
},
{
name: 'textarea name',
value: 'value'
}
]
...and again leave transforming this into a different structure as an exercise for the reader.
Give me teh codez already!
var serialize = (function (slice) {
return function (form) {
//no form, no serialization
if (form == null)
return null;
//get the form elements and convert to an array
return slice.call(form.elements)
.filter(function (element) {
//remove disabled elements
return !element.disabled;
}).filter(function (element) {
//remove unchecked checkboxes and radio buttons
return !/^input$/i.test(element.tagName) || !/^(?:checkbox|radio)$/i.test(element.type) || element.checked;
}).filter(function (element) {
//remove <select multiple> elements with no values selected
return !/^select$/i.test(element.tagName) || element.selectedOptions.length > 0;
}).map(function (element) {
switch (element.tagName.toLowerCase()) {
case 'checkbox':
case 'radio':
return {
name: element.name,
value: element.value === null ? 'on' : element.value
};
case 'select':
if (element.multiple) {
return {
name: element.name,
value: slice.call(element.selectedOptions)
.map(function (option) {
return option.value;
})
};
}
return {
name: element.name,
value: element.value
};
default:
return {
name: element.name,
value: element.value || ''
};
}
});
}
}(Array.prototype.slice));
You could go for a manual way (Below code is not tested though);
var form = document.getElementsByTagName("form");
var inputs = form[0].getElementsByTagName("input");
var formData = {};
for(var i=0; i< inputs.length; i++){
formData[inputs[i].name] = inputs[i].value;
}
var formdata = JSON.stringify(formData);
if you use a library this would be easier.
Eg:- in jQuery:
var formObjects = $("form :input");
formObjects.each(
function(){
formData[$(this).attr("name")] = $(this).val(); /*setting up name/value pairs */
}
);
in this code formObjects contains all input, select and textarea and other form element tags. so we don't need to manually search for each like in plain JS. Except for radio buttons (As #enhzflep implied this doesn't work correct for input[type=radio] )
But if you use jQuery you can directly use jQuery's serialize() function to grab the whole form by name-value pairs.
var url_friendly_name_value_string = $("form").serialize();
If you need object of {key: value, ...}
const form = document.getElementById("your-form")
const formEntries = new FormData(form).entries();
const formData = Object.assign(...Array.from(formEntries, ([name, value]) => ({[name]: value})));
console.log(formData)
You would need to manually generate the json/javascript object before sending it to the server.
Should be a method called onSubmit which would pick each of the form's input value and create an object which can then be sent to the server page.
You can refer this answer which is similar to what you are looking for:
Convert form data to JavaScript object with jQuery
Just use jQuery.
HTML:
<form id="my-form">
<input type="text" name="my-field" />
</form>
JS:
var data = $('form#my-form').serializeArray(); // [{'my-field': 'value'}]
$.ajax({
data: data
//other config goes here
});
Pardon my haste for not formatting the example, But its good to understand. Click 'Get All Values' to see the key/value pair in alert.
JS Fiddle
$(function(){
$(".getFormVal").on("click",function(e){
e.preventDefault();
var formObj={};
var formEle = $(".form").find("input:not([type=submit],[type=button]),select,textarea");
$(formEle).each(function(){
if($(this).prop("tagName").toLowerCase() == "input"){
if( ($(this).attr("type").toLowerCase() == "text") ||
($(this).attr("type").toLowerCase() == "radio" && $(this).is(":checked")) ||
($(this).attr("type").toLowerCase() == "file") ){
formObj[$(this).attr("name")] = $(this).val();
}else if( $(this).attr("type").toLowerCase() == "checkbox" && $(this).is(":checked") ){
if(formObj[$(this).attr("name")] === undefined){
formObj[$(this).attr("name")] = [];
}
formObj[$(this).attr("name")].push($(this).val());
}
}else if( $(this).prop("tagName").toLowerCase() == "textarea" ){
formObj[$(this).attr("name")] = $(this).val();
}else if( $(this).prop("tagName").toLowerCase() == "select" ){
if($(this).attr("multiple")){
if(formObj[$(this).attr("name")] === undefined){
var selectEleName = $(this).attr("name");
formObj[selectEleName] = [];
}
$('option:selected',this).each(function(i, selected){
formObj[selectEleName].push($(this).attr("value"));
});
}else{
formObj[$(this).attr("name")] = $(this).val();
}
}
});
alert(JSON.stringify(formObj));
});
});
I'm suggesting you to use css selectors for selecting the inputs from your form: querySelector and querySelectorAll.
Here's the working code and here's the js code.
function get_form_data()
{
var el = document.querySelector('form');
var matches = el.querySelectorAll('input[name]');
var data = {};
for(var i=0; i< matches.length; i++){
data[matches[i].name] = matches[i].value;
}
var json_data = JSON.stringify(data);
}
Sure you can modify the selectors for your needs.
I hope this will be useful and good luck to you.
Here is the pure javascript solution which gives the required output for all kind of inputs in a form including files.
Sample output
{
"firstname":"John",
"surname":"doe",
"email":"abc#gmail.com",
"gender":"female",
"subscribe":true,
"selection":"M",
"description":"sdfs sdfs sdf",
"upload":"data:text/plain;base64,/v8JLAk+CTMAIAkVCUcJNgk1ACAJIAk+CRUJMAlHAC4ADQAKAA0ACgk5CT8JAgkoCU0JJglBCSQJTQk1CT4JGgk+ACAJHwkjCSQJTQkVCT4JMAAuACAJNQlNCS8JAgkXCRoJPwkkCU0JMAkVCT4JMAAgCSQJRwAgCTkJPwkoCU0JJglBCTkJTQkwCUEJJgkvCTgJLglNCTAJPgkfAA0ACgkkCUsJAgkhACAJNQk+CRwJNQlBCSgAIAkoCU0JLwk+CS8AIAkuCT8JMwkkACAJKAk4CUcJMgAgCSQJMAANAAoJJAlLCQIJIQk+CSQAIAk1CT4JHAk1CUEJKAAgCSgJTQkvCT4JLwAgCS4JPwkzCTUJPgAsAA0ACgkqCSMAIAkoCU0JLwk+CS8AIAk5CT4AIAkdCT4JMgk+CRoAIAkqCT4JOQk/CRwJRwAuAA0ACgkPCRUAIAkuCT4JIwlBCTgAIAkkCUEJLgkaCU0JLwk+ACAJLgk+CR0JTQkvCT4AIAk4CT4JMAkWCT4JGgAsACAJCQkCCRoJQAkoCUcAIAkGCSMJPwAgCTUJHAkoCT4JKAlHACAJLgk+CSQJTQkwACAJFQkuCUAJGgAuACAJKgk+CTkJJAAgCTkJSwkkCT4AIAkGCSoJMglNCS8JPgAgCQYJHAlBCSwJPgkcCUEJGglAACAJKgkwCT8JOAlNCSUJPwkkCUAALgAgCTkJSwkjCT4JMAk+ACAJLgkwCT4JIAlAACAJLgk+CSMJOAk+CTUJMAkaCT4AIAkFCSgJTQkvCT4JLwAsACAJJAlLCTkJPwAgCS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gCTYJFQkkCUsALAAgCSQJRwk5CT8AIAkFCSgJRwkVACAJNQkwCU0JNwlHACAJOAkyCRcAIAk5CUcAIAkFCRwJQQkoACAJKAAgCTgJQQkfCTIJRwkyCUcAIAkVCUsJIQlHCRoALgAgCS4JTQk5CSMJQQkoCRoAIAksCT4JMwk+CTgJPgk5CUcJLAAgCSAJPgkVCTAJRwAgCTkJRwAgCSgJQQk4CSQJRwAgCTUJTQkvCRUJTQkkCT8JLgkkCU0JNQAgCSgJOAlBCSgAIAkPCRUAIAkaCS4JJAlNCRUJPgkwACAJBgk5CUcJJAAuAA0ACgkuCTAJPgkgCT8AIAkFCTgJTQkuCT8JJAlHCRoJQAAgCQYJFwAgCRwJTQkvCT4JKAlHACAJLgk5CT4JMAk+CTcJTQkfCU0JMAk+CSQJMglNCS8JPgAgCS4JMAk+CSAJPwAgCS4JPgkjCTgJPgkkACAJJwkXCScJFwkkACAJIAlHCTUJMglAACAJBQk2CU0JLwk+ACAJNQlNCS8JFQlNCSQJPwkuCSQJTQk1CT4JOAAgCS4JPgkdCUcAIAkqCU0JMAkjCT4JLgAsACAJJAlNCS8JPgkCCRoJPgAgCQYJJgkwCU0JNgAgCRgJRwkKCSgAIAkuCTkJPgkwCT4JNwlNCR8JTQkwACAJGAkhCTUJPgkvCRoJPgAgCSoJTQkwCS8JJAlNCSgAIAkVCTAJQQkvCT4ALgANAAoJHAkvACAJLgk5CT4JMAk+CTcJTQkfCU0JMAAuAA0ACg=="
}
function getData(jsonForm){
var el = document.querySelector('form[name='+jsonForm+']');
var inputs = el.querySelectorAll('input,select,textarea');
var data = {};
for(var i=0; i< inputs.length; i++){
switch(inputs[i].type){
case 'file':
var file = inputs[i].files[0];
if(file){
var oReader = new FileReader();
(function(i){
oReader.onload = function(e){
data[inputs[i].name] = e.target.result;
alert(JSON.stringify(data));
document.querySelector('p').innerHTML = JSON.stringify(data);
console.dir(data);
};
oReader.readAsDataURL(file);
})(i)
}
break;
case 'checkbox':
data[inputs[i].name] = inputs[i].checked;
break;
default:
data[inputs[i].name] = inputs[i].value;
}
}
document.querySelector('p').innerHTML = JSON.stringify(data);
}
form{width: 400px}
<form name="jsonForm">
<input type="text" name="firstname" value="John" />
<input type="text" name="surname" value="doe" />
<input type="email" name="email" value="abc#gmail.com" />
M: <input type="radio" name="gender" value="male" checked/>
F: <input type="radio" name="gender" value="female" />
subscribe: <input type="checkbox" name="subscribe" checked/>
<input type="file" name="upload" />
<select name="selection"><option value=M>M</option><option value=J>J</option></select>
<textarea name="description"></textarea>
</form>
<hr/>
<input type=button value="getData" onclick=getData('jsonForm')>
<p><p>
Late to the party, but this is how I do it:
(function getFormData(){
var form, inputs, formData = {}, outputdiv;
(function init(){
outputdiv = document.getElementById("output");
output();
})();
function build(){
form = document.querySelector("form"),
inputs = form.querySelectorAll("input, select, textarea"),
formData = {},
arr = [];
for(var i=0; i< inputs.length; i++){
var input = inputs[i];
input.onchange = function(){output();};
if(input.type=='radio'){
if(input.checked){
arr.push(input.name,input.value);
}
} else if(input.type=='checkbox'){
if(input.checked){
arr.push(input.name,input.value);
}
} else if(input.multiple){
//find each selected child
var options = input && input.options;
for (var o=0; o<options.length; o++) {
var opt = options[o],
val = (opt.value || opt.text)
if (opt.selected) {
arr.push(input.name,val);
}
}
} else {
arr.push(input.name,input.value);
}
}
var formdata = JSON.stringify(arr);
return formdata;
}
function output(){
outputdiv.innerHTML = build();
}
})()
<form>
<input type="text" name="text1" value="" />
<input type="text" name="text2" value="" />
<input type="email" name="text3" value="" />
<br />
<input type="radio" name="radio" value="radio1" />
<input type="radio" name="radio" value="radio2" />
<input type="radio" name="radio" value="radio3" />
<br />
<input type="checkbox" name="checkboxes" value="checkbox1" />
<input type="checkbox" name="checkboxes" value="checkbox2" />
<input type="checkbox" name="checkboxes" value="checkbox3" />
<br />
<textarea name="textarea"></textarea>
<br />
<select name="option">
<option value="">options</option>
<option>option text</option>
<option value="option 2 value">option 2 text</option>
<option>option 3 text</option>
</select>
<br />
<select multiple="true" name="options">
<option>opt multi 1</option>
<option value="opt multi 2 value">opt multi 2</option>
<option>opt multi 3</option>
</select>
</form>
<div id="output"></div>

How can send array as an data using ajax

Please see attached jsfiddle link, I need to collect data from multiple forms and combined the data as Single data array send it to server(spring mvc controller) to persist using ajax.post
Please let me know best way to do it, will my array be converted to Json by ajax call or I have to do some magic on it.
Thanks
http://jsfiddle.net/6jzwR/1/
<form id="form1" name="formone" class="myclass">
<input type="text" id="txt11" name="txt11" value="name1" />
<input type="text" id="txt12" name="txt12" value="name2" />
</form>
<form id="form1" name="formtwo" class="myclass">
<input type="text" id="txt21" name="txt21" value="name3" />
<input type="text" id="txt22" name="txt22" value="name4" />
</form>
<input type="button" id="button" value="Click Me" />
(function ($) {
$(document).ready(function () {
alert("serialize data :" + $('.myclass').length);
var mydata = null;
$('#button').on('click', function (e) {
$('.myclass').each(function () {
alert("serialize data :" + $(this).serialize());
if ((mydata === null) || (mydata === undefined)) {
mydata = $(this).serializeArray();
alert("My data is null");
} else {
mydata = $.merge(mydata, $(this).serializeArray());
alert("My data final data after merger " + test);
}
});
});
});
}(jQuery));
Try this:
var array = $('input[type="text"]').map(function() {
return $(this).val();
}).get();
alert(JSON.stringify(array));
Demo.
You can put all the forms' data in an array and join them with &
var formdata = []
$('.myclass').each(function(){
formdata.push($(this).serialize());
});
var data = formdata.join('&');
http://jsfiddle.net/6jzwR/3/

Categories