What I am trying to do is to create a JSON from html form. Then to use this script (Formulator - https://github.com/walpolea/Formulator) to visualize it.
I can use serializeArray() to export values and names to JSON.
However, for the Formulator to work, I have to create the JSON with a specific structure that looks like this.
{
"custom1":"some value",
"custom2":"some value",
"inputs":[
{
"label":{
"content":"Input: ",
"class":"inputLabel"
},
"id":"first_name",
"name":"first_name",
"type":"text",
"size":"50"
},
{
"label":{
"content":"Select: ",
"class":"inputLabel"
},
"id":"FirstSelect",
"name":"FirstSelect",
"type":"select",
"options":[
{
"value":"One"
},
{
"value":"Two"
}
]
},
{
"label":{
"content":"Checkboxes: ",
"class":"inputLabel"
},
"id":"checkbox",
"name":"somevalue",
"type":"checkbox",
"options":[
{
"value":"Dog",
"name":"pet"
},
{
"value":"Cat",
"name":"pet"
},
{
"value":"Bird ",
"name":"pet"
}
]
},
{
"id":"submit",
"name":"submit",
"type":"submit",
"value":"Submit"
}
]
}
My problem comes in the "inputs":[] section of that JSON.
I can't figure out how to do it. Also, in the case of a select element, it has to take all the options instead of only the selected one. The same is valid also for the checkboxes.
Here is DEMO: https://jsfiddle.net/largan/avz672s4/71/
$(document).ready(function() {
$('#submit').on('click', function () {
$('#result').html('');
$('#result').append(
$('<pre>').text(JSON.stringify(_get_values($('#form')), null, 2))
)
event.preventDefault();
});
});
function _get_values(form)
{
let data = {
"custom1":"some value",
"custom2":"some value"
};
$(form).find('input, textarea, select').each(function(x, field) {
if (field.name) {
if (field.name.indexOf('[]') > 0) {
if (!$.isArray(data[field.name])) {
data[field.name] = new Array();
data['type'] = 'select';
}
for (let i = 0; i < field.selectedOptions.length; i++) {
data[field.name].push(field.selectedOptions[i].value);
}
} else {
data[field.name] = field.value;
data['type'] = 'input';
}
}
});
return data
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Convert form to JSON</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"> </script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"> </script>
<script src="https://code.jquery.com/jquery-1.11.3.js"> </script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.js"> </script>
</head>
<body>
<div class="container">
<h2>Form to JSON</h2>
<p></p>
<form id="form">
<div class="form-group">
<label for="first_name" class="inputLabel">Input:</label>
<input type="text" name="first_name" id="first_name" class="form-control" size="50">
<br>
<label for="select" class="inputLabel">Select:</label>
<select multiple class="form-control" id="FirstSelect" name="FirstSelect[]" size="2">
<option>One</option>
<option>Two</option>
</select>
<br>
<label for="select" class="inputLabel">Select:</label>
<select multiple class="form-control" id="SecondSelect" name="SecondSelect[]" size="3">
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<br>
<label for="pet" class="inputLabel">Checkboxes:</label>
<div class="form-check-inline">
<input type="checkbox" id="checkbox[]" name="pet" value="Dog" /> Dog
</div>
<div class="form-check-inline">
<input type="checkbox" id="checkbox[]" name="pet" value="Cat" /> Cat
</div>
<div class="form-check-inline">
<input type="checkbox" id="checkbox[]" name="pet" value="Bird" /> Bird
</div>
</div>
<button id="submit" name="submit" type="submit" class="btn btn-primary">Submit</button>
</form>
<p></p>
<div class="alert alert-info" id="result" ></div>
</div>
</body>
</html>
What I want to achieve is to create the required JSON once I click on Submit