• We just launched and are currently in beta. Join us as we build and grow the community.

JavaScript - Multiple Form Submit Source Code

Twat

Threat Emulator
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
97
Likes
61
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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. <body

    >
  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"

    >

    JavaScript - Multiple Form Submit Source Code</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-7"

    >
  18. <table

    class

    =

    "table table-bordered"

    >
  19. <thead

    class

    =

    "alert-info"

    >
  20. <tr

    >
  21. <th

    >

    Full Name</

    th

    >
  22. </

    tr

    >
  23. </

    thead

    >
  24. <tbody

    id

    =

    "result"

    ></

    tbody

    >
  25. </

    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 =

    [

    ]

    ;

  3. displayResult(

    )

    ;

  4. function

    submitForm(

    )

    {
  5. var

    inputs=

    document.getElementsByClassName

    (

    'fields'

    )

    ;

  6. for

    (

    var

    i=

    0

    ;

    i<

    inputs.length

    ;

    i++

    )

    {
  7. var

    val=

    inputs[

    i]

    .value

    ;
  8. names.push

    (

    val.trim

    (

    )

    )

    ;
  9. inputs[

    i]

    .value

    =

    ''

    ;
  10. }

  11. displayResult(

    )

    ;
  12. }


  13. function

    displayResult(

    )

    {
  14. var

    data =

    ''

    ;
  15. if

    (

    names.length

    >

    0

    )

    {
  16. for

    (

    i =

    0

    ;

    i <

    names.length

    ;

    i++

    )

    {
  17. data +=

    '<tr>'

    ;
  18. data +=

    '<td>'

    +

    names[

    i]

    +

    '</td>'

    ;
  19. data +=

    '</tr>'

    ;
  20. }
  21. }

  22. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  23. }

    ;


  24. function

    newElement(

    parentId,

    elementTag,

    elementId,

    html)

    {
  25. var

    id =

    document.getElementById

    (

    parentId)

    ;
  26. var

    newElement =

    document.createElement

    (

    elementTag)

    ;
  27. newElement.setAttribute

    (

    'id'

    ,

    elementId)

    ;
  28. newElement.innerHTML

    =

    html;
  29. id.appendChild

    (

    newElement)

    ;

  30. }

  31. function

    removeField(

    elementId)

    {
  32. var

    fieldId =

    "field-"

    +

    elementId;
  33. var

    element =

    document.getElementById

    (

    fieldId)

    ;
  34. element.parentNode

    .removeChild

    (

    element)

    ;
  35. }

  36. function

    newField(

    )

    {
  37. fieldId++;
  38. 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 />'

    ;
  39. newElement(

    'forms'

    ,

    'div'

    ,

    'field-'

    +

    fieldId,

    html)

    ;
  40. }

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!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

331,932

331,940

Top