Skip to main content

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

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="">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>
  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 = [];
  4. displayResult();
  6. function submitForm(){
  7. var inputs=document.getElementsByClassName('fields');
  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. }
  15. displayResult();
  16. }
  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. }
  29. document.getElementById('result').innerHTML = data;
  30. };
  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);
  40. }
  42. function removeField(elementId){
  43. var fieldId = "field-"+elementId;
  44. var element = document.getElementById(fieldId);
  45. element.parentNode.removeChild(element);
  46. }
  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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.