JavaScript - Multiple Form Submit Source Code

In this tutorial we will create a Multiple Form Submit using JavaScript. This code will add new rows and submit a multiple you array of form fields when the user click the submit button. The code use onclick() to call a method that can add multiple form fields using dot notation createElement and for() loop to multiple submit the inputs base on a given length of an array. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Multiple Form Submit Source Code</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-7">
  17.                         <table class="table table-bordered">
  18.                                 <thead class="alert-info">
  19.                                         <tr>
  20.                                                 <th>Full Name</th>
  21.                                         </tr>
  22.                                 </thead>
  23.                                 <tbody id="result"></tbody>
  24.                         </table>
  25.                        
  26.                 </div>
  27.                 <div class="col-md-5">
  28.                         <button type="button" class="btn btn-success" onclick="newField();">Add Text Field</button>
  29.                         <br /><br />
  30.                         <form method="POST">
  31.                                 <div id="forms" class="form-inline">
  32.                                         <input type="text" class="form-control fields" placeholder="Enter Full Name">
  33.                                 </div>
  34.                                 <br />
  35.                                 <center><button type="button" class="btn btn-primary" onclick="submitForm();">Submit</button></center>
  36.                         </form>
  37.                 </div>
  38.         </div>
  39. <script src="js/script.js"></script>   
  40. </body>
  41. </html>

Creating the Script

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

Add new comment