How To Assign Data From Html Form(in Json Format) To Variable? May 30, 2024 Post a Comment In my abc.html I have the following code which will covert the form data(hard coded for now) to JSON format: Solution 1: If you are not handling this in backend you can retrieve the data in Javascript in a variable then just appned it as JSON to your data using JSON.stringify().This is a working snippet:functionsubmitform() { varFormData = { Company: myForm.Company.value, User: myForm.User.value, parameterMap: { p1: myForm.p1.value, p2: myForm.p2.value, p3: myForm.p3.value } }; console.log(FormData); $.ajax({ url: "myurl", type: 'POST', dataType: 'json', data: JSON.stringify(FormData), contentType: 'application/json', mimeType: 'application/json', success: function(data) { alert(data.uuid); }, error: function(data, status, er) { alert("error: " + data + " status: " + status + " er:" + er); } }); }Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formenctype='application/json'method="POST"name="myForm"><p><label>Company:</label><inputname='Company'value='TESTCOMPANY'></p><p><label>User Id:</label><inputname='User'value='TESTUSER'></p><p><label>Division:</label><inputtype="text"name='p1'value='12345'></p><p><label>From:</label><inputtype="text"name='p2'value='20-MAR-2016'></p><p><label>To:</label><inputtype="text"name='p3'value='22-MAR-2016'></p><inputvalue="Submit"type="submit"onclick="submitform()"></form>CopyNotes:Use Javascript naming conventions, for example FormData will better be formData.Solution 2: Using the provided HTML structure you could get the data into the format using:functionsubmitform(){ var form = document.querySelector('form'); var result = {}; var input = form.getElementsByTagName('input'); for(var i = 0; i < input.length; i ++) { var row = input[i]; var rowName = ((row.name || '').match(/(\w+)(\[(\w+)\])?/) || []); var rowIndex = rowName[3]; rowName = rowName[1]; if(rowName) { var typeOfRowIndex = typeof rowIndex; var rowValue = row.value; if(typeof result[rowName] === 'undefined') { var rowToAdd = {}; rowToAdd[rowIndex] = rowValue; result[rowName] = typeOfRowIndex === 'undefined' ? rowValue : rowToAdd; } elseif(typeOfRowIndex !== 'undefined') { result[rowName][rowIndex] = rowValue; } } } document.getElementById('output').innerHTML = JSON.stringify(result) console.log(result); returnfalse; }Copy<body><formenctype='application/json'method="POST"name="myForm"><p><label>Company:</label><inputname='Company'value='TESTCOMPANY'></p><p><label>User Id:</label><inputname='User'value='TESTUSER'></p><p><label>Division:</label><inputtype="text"name='parameterMap[p1]'value='12345'></p><p><label>From:</label><inputtype="text"name='parameterMap[p2]'value='20-MAR-2016'></p><p><label>To:</label><inputtype="text"name='parameterMap[p3]'value='22-MAR-2016'></p><inputvalue="Submit"type="submit"onclick="return submitform()"></form><p>JSON Output (after clicking submit button):</p><divid="output"></div><p>Can also check console to see JSON object (after clicking submit button)</p></body>CopySolution 3: varFormData = $('form[name=myForm]').serializeArray(); FormData = JSON.stringify(FormData); CopySolution 4: I don't like jQuery, but:var $inputs = $('form[name="myForm"]').find('input'); var res = {}; Array.prototype.forEach.call($inputs, function($item, index) { res[ $item.getAttribute('name') ] = $item.value; }); varFormData = JSON.stringify(res); alert(FormData);Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><body><formenctype='application/json'method="POST"name="myForm"><p><label>Company:</label><inputname='Company'value='TESTCOMPANY'></p><p><label>User Id:</label><inputname='User'value='TESTUSER'></p><p><label>Division:</label><inputtype="text"name='parameterMap[p1]'value='12345' ></p><p><label>From:</label><inputtype="text"name='parameterMap[p2]'value='20-MAR-2016'></p><p><label>To:</label><inputtype="text"name='parameterMap[p3]'value='22-MAR-2016'></p><inputvalue="Submit"type="submit"onclick="submitform()"></form></body>Copy Share Post a Comment for "How To Assign Data From Html Form(in Json Format) To Variable?"
Post a Comment for "How To Assign Data From Html Form(in Json Format) To Variable?"