JavaScript - Submit Multiple Fields

In this tutorial we will create a Submit Multiple Fields using JavaScript. This code will dynamically insert a multiple you array of form fields when the user click the insert button. The code use onclick() to call a method that can insert a multiple form fields with the use of for() loop to repeatedly insert the inputs base on the length of the array index. 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 - Submit Multiple Fields</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-5">
  17.                         <button type="button" class="btn btn-success" onclick="addField();">Add Field</button>
  18.                         <br /><br />
  19.                         <form method="POST">
  20.                                 <div id="forms" class="form-inline">
  21.                                         <input type="text" class="form-control fields" placeholder="Enter Full Name">
  22.                                 </div>
  23.                                 <br />
  24.                                 <center><button type="button" class="btn btn-primary" onclick="submitForm();">Insert</button></center>
  25.                         </form>
  26.                 </div>
  27.                 <div class="col-md-7">
  28.                         <table class="table table-bordered">
  29.                                 <thead class="alert-info">
  30.                                         <tr>
  31.                                                 <th>Full Name</th>
  32.                                         </tr>
  33.                                 </thead>
  34.                                 <tbody id="result"></tbody>
  35.                         </table>
  36.                 </div>
  37.         </div>
  38. <script src="js/script.js"></script>   
  39. </body>
  40. </html>

Creating the Script

This code contains the script of the application. This code will insert 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. displayAll();
  5.  
  6. function displayAll() {
  7.         var data = '';
  8.         if (names.length > 0) {
  9.                 for (i = 0; i < names.length; i++) {
  10.                         data += '<tr>';
  11.                         data += '<td>' + names[i] + '</td>';
  12.                         data += '</tr>';
  13.                 }
  14.         }
  15.  
  16.         document.getElementById('result').innerHTML = data;
  17. };
  18.  
  19.  
  20. function addElement(parentId, elementTag, elementId, html){
  21.         var id = document.getElementById(parentId);
  22.         var newElement = document.createElement(elementTag);
  23.         newElement.setAttribute('id', elementId);
  24.         newElement.innerHTML = html;
  25.         id.appendChild(newElement);
  26.  
  27. }
  28.  
  29. function removeField(elementId){
  30.         var fieldId = "field-"+elementId;
  31.         var element = document.getElementById(fieldId);
  32.         element.parentNode.removeChild(element);
  33. }
  34.  
  35. function addField(){
  36.         fieldId++;
  37.         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-minus"></span></button><br />';
  38.         addElement('forms', 'div', 'field-'+ fieldId, html);
  39. }
  40.  
  41. function submitForm(){
  42.         var inputs=document.getElementsByClassName('fields');
  43.        
  44.         for(var i=0; i<inputs.length; i++){
  45.                         var val=inputs[i].value;
  46.                         names.push(val.trim());
  47.                         inputs[i].value = '';
  48.         }
  49.        
  50.         displayAll();
  51. }
There you have it we successfully created a Submit Multiple Fields 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!

Comments

Thanks for this tutorial

Add new comment