JS Form Generator

Discussion in 'JavaScript' started by alfman05302001, Feb 10, 2008.

  1. #1
    So I am not sure if this is possible (and if it is how to even go about doing it) but below is what I am trying to do.

    What I would like to do is create a page that automatically creates a new set of identical form entry boxes once the set above it was completed.

    I am trying to make a registration form submitted via PHP to a MySQL table. However one person will be registering a group of people but the number of people being registered is variable. I was wondering if there was a clean and simple way to make the number of entry forms vary to the number of people who being registered by the individual.

    Sorry for sounding so clueless JS isn't a language I am knowledgeable with, but seems like it would be the best choice from the little I know.

    Jonathan
     
    alfman05302001, Feb 10, 2008 IP
  2. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What you need to do is obtain a list of all of the inputs inside the form. So assign an ID to your form, we'll just call it "form" for now.

    This code should work.

    <script type="text/javascript">
    // variables
    var form = document.getElementById('form');
    var inputs = form.childNodes;
    // create new form
    var new_form = document.createElement('form');
    // clone attributes
    new_form.action = form.action;
    new_form.enctype = form.enctype;
    new_form.method = form.method;
    form.parentNode.appendChild(new_form);
    // loop through inputs and clone each one
    for (var i = 0; i < inputs.length; i++) {
    var new_input = document.createElement('input');
    new_input.name = inputs[i].name;
    new_input.value = inputs[i].value;
    new_form.appendChild(new_input);
    }
    </script>
    Code (markup):
    Try that out, let me know if it works :)
     
    -NB-, Feb 11, 2008 IP
  3. alfman05302001

    alfman05302001 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply

    I am going to test out the code and see if I can get it to work. http://oa.ppbsa.org/Database_Testing/election_submission/election_form.php
    I will let you know if I can get it to work. Thanks
     
    alfman05302001, Feb 13, 2008 IP