How to Submit Data with Multiple Form in JavaScript

How to Submit Data with Multiple Form in JavaScript

Introduction

In this tutorial we will create a How to Submit Data with Multiple Form in JavaScript. This tutorial purpose is to teach how you can submit multiple forms. This will tackle all the important functionality that will let you submit all your form in one submission. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you have more forms. I will give my best to provide you the easiest way of creating this program Multiple Form Submit. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html forms and data result. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body onload="displayResult();">
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">How to Submit Data with Multiple Form</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-5">
  18.                         <button type="button" class="btn btn-success" onclick="newField();"><span class="glyphicon glyphicon-plus"></span></button>
  19.                         <br /><br />
  20.                         <form method="POST">
  21.                                 <div id="forms" class="form-inline">
  22.                                         <input type="text" class="form-control fields" placeholder="Enter something here">
  23.                                 </div>
  24.                                 <br />
  25.                                 <button type="button" class="btn btn-primary" onclick="submitForm();">Submit form</button>
  26.                         </form>
  27.                 </div>
  28.                 <div class="col-md-7">
  29.                         <table class="table table-bordered">
  30.                                 <thead class="alert-info">
  31.                                         <tr>
  32.                                                 <th>Data List</th>
  33.                                         </tr>
  34.                                 </thead>
  35.                                 <tbody id="result"></tbody>
  36.                         </table>
  37.                        
  38.                 </div>
  39.         </div>
  40. <script src="script.js"></script>      
  41. </body>
  42. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will let your submit multiple form instances when you click the button To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let fieldId = 0;
  2. let names = [];
  3.  
  4.  
  5. function displayResult() {
  6.         let data = '';
  7.         if (names.length > 0) {
  8.                 for (i = 0; i < names.length; i++) {
  9.                         data += '<tr>';
  10.                         data += '<td>' + names[i] + '</td>';
  11.                         data += '</tr>';
  12.                 }
  13.         }
  14.  
  15.         document.getElementById('result').innerHTML = data;
  16. };
  17.  
  18.  
  19. function newElement(parentId, elementTag, elementId, html){
  20.         const id = document.getElementById(parentId);
  21.         const newElement = document.createElement(elementTag);
  22.         newElement.setAttribute('id', elementId);
  23.         newElement.innerHTML = html;
  24.         id.appendChild(newElement);
  25.  
  26. }
  27.  
  28. function removeField(elementId){
  29.         const fieldId = "field-"+elementId;
  30.         const element = document.getElementById(fieldId);
  31.         element.parentNode.removeChild(element);
  32. }
  33.  
  34. function newField(){
  35.         fieldId++;
  36.         let html = '<br /><input type="text" class="form-control fields" placeholder="Enter something here">' + '<button class="btn btn-danger btn-sm" onclick="removeField('+fieldId+');"><span class="glyphicon glyphicon-trash"></span></button><br />';
  37.         newElement('forms', 'div', 'field-'+ fieldId, html);
  38. }
  39.  
  40. function submitForm(){
  41.         const inputs=document.getElementsByClassName('fields');
  42.        
  43.         for(var i=0; i<inputs.length; i++){
  44.                         var val=inputs[i].value;
  45.                         names.push(val.trim());
  46.                         inputs[i].value = '';
  47.         }
  48.        
  49.         displayResult();
  50. }

In the code provided we uses multiple methods to dynamically add new form fields for submitting our forms. To submit multiple forms we will create a method called submitForm() This function will immediately submit all the input fields within the forms. We use for loop to loop through all the inputs inside the form by breaking down each inputs.

Output:


The How to Submit Data with Multiple Form in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Submit Data with Multiple Form in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Comments

Nice .In fact it is a good
JOB DONE

Add new comment