Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple fields to form

I would like to add a function that generates multiple fields to my form.

This is how my form looks like:

<form action="form_sent.php" method="post">
    <input name="name" type="text" placeholder="Name"><br>
   <input name="phone" type="text" placeholder="Phone"><br>
   <input name="email" type="text" placeholder="E-Mail"><br><br>
   <button>Add more fields</button><br><br>
   <input type="submit">   
</form>

In my case I want 3 new fields (name, phone, email) when clicking on "Add more fields".

How can I do this?

https://jsfiddle.net/374cxt5s/

like image 826
Pom Canys Avatar asked Dec 30 '15 16:12

Pom Canys


People also ask

How to add multiple inputs in javascript?

var counter = 1; //limits amount of transactions function addElements() { if (counter < 5) //only allows 4 additional transactions { let div = document. createElement('div'); div.id = 'row' + counter; document. body. appendChild(div); let input = document.

How Select multiple fields in jotform?

To manage multiple fields, hold down the Ctrl key (for PC), or the Command ⌘ key (for Mac) to multi-select fields. You can also press Ctrl + A or ⌘ + A to select all fields. A menu will appear at the top that will allow you to manage the multiple fields you selected.

How do I add a space between forms?

To create extra spaces before, after, or in-between your text, use the &nbsp; (non-breaking space) extended HTML character. For example, with the phrasing "extra space" using a double space, we have the following code in our HTML.


2 Answers

Try this: https://jsfiddle.net/Twisty/q8zj00s0/1/

HTML

<form action="form_sent.php" method="post">
  <ul id="fieldList">
    <li>
      <input name="name[]" type="text" placeholder="Name" />
    </li>
    <li>
      <input name="phone[]" type="text" placeholder="Phone" />
    </li>
    <li>
      <input name="email[]" type="text" placeholder="E-Mail">
    </li>
  </ul>
  <button id="addMore">Add more fields</button>
  <br>
  <br>
  <input type="submit">
</form>

CSS

ul {
  padding: 0;
  margin: 0;
}

ul li {
  list-style: none;
}

JQuery

$(function() {
  $("#addMore").click(function(e) {
    e.preventDefault();
    $("#fieldList").append("<li>&nbsp;</li>");
    $("#fieldList").append("<li><input type='text' name='name[]' placeholder='Name' /></li>");
    $("#fieldList").append("<li><input type='text' name='phone[]' placeholder='Phone' /></li>");
    $("#fieldList").append("<li><input type='text' name='email[]' placeholder='E-Mail' /></li>");
  });
});

This allows you to store the results in array when you submit the form. Since you could have 5 names, phones, and emails, an array is the best way to address that. Then in PHP, you would have $_POST['name'][0] as the first one.

like image 70
Twisty Avatar answered Oct 10 '22 16:10

Twisty


I'm assuming you probably want to create a dynamic form that allows you to add multiple contacts, etc.

CodePen Example

http://codepen.io/anon/pen/yeVRgw

The Basic HTML Setup

So that you can loop through things, and for sake of your own sanity, you'll probably want to segment out each chunk within the form. We'll also set up a hidden input to track how many partitions of name,phone,email have been created. We'll default at 1

<form action="form_sent.php" method="POST">
    <input type="hidden" name="contacts" id="contacts" value="1">

    <div class="form-contacts-container">

        <div class="form-contact" id="form-contact-1">
            <input type="text" name="name-1" id="name-1" placeholder="Name">
            <input type="text" name="email-1" id="email-1" placeholder="E-mail">
            <input type="text" name="phone-1" id="phone-1" placeholder="Phone">
        </div>

        <!-- We'll be adding additional inputs here -->

    </div>

    <div class="form-contacts-add">
        <input type="button" value="Add More Fields" id="add-fields">
    </div>

    <div class="form-contacts-submit">
        <input type="submit" name="submit" id="submit" value="Submit">
    </div>

</form>

The JavaScript

This assumes you are using jQuery, so ensure that this is in your <head>

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

Now we need to do a few things - firstly, attach an event listener to our button and secondly, add a new <div class="form-contact"> with included fields to our form. We'll also need to ensure that we're counting up to make sure each section has a unique name/id, and we'll increase the hidden input value to count how many contacts have been added in total.

<script type="text/javascript">
    var total = 1; // Our default for how many contacts we have


    $( document ).on( 'click', '#add-fields', function() {

        var addBlockId = total = total + 1;

        var addBlock = document.createElement('div');
        $(addBlock).addClass('form-contact');
        $(addBlock).attr('id','form-contact-' + addBlockId);

        var inputName = document.createElement('input');
        $(inputName).attr('type','text');
        $(inputName).attr('name','name-' + addBlockId);
        $(inputName).attr('id','name-' + addBlockId);
        $(inputName).attr('placeholder','Name');
        $(inputName).appendTo($(addBlock));

        var inputEmail = document.createElement('input');
        $(inputEmail).attr('type','text');
        $(inputEmail).attr('name','email-' + addBlockId);
        $(inputEmail).attr('id','email-' + addBlockId);
        $(inputEmail).attr('placeholder','E-mail');
        $(inputEmail).appendTo($(addBlock));

        var inputPhone = document.createElement('input');
        $(inputPhone).attr('type','text');
        $(inputPhone).attr('name','phone-' + addBlockId);
        $(inputPhone).attr('id','phone-' + addBlockId);
        $(inputPhone).attr('placeholder','Phone');
        $(inputPhone).appendTo($(addBlock));

        $(addBlock).appendTo($('.form-contacts-container'));
        $('#contacts').val(total);

    });

</script>

Processing your Form

The last piece of the puzzle is to process your form properly. Not goign to give you all the answers here, but the basic logic would be to grab the $_POST['contacts'] value we've been updated and run a loop through to grab all of your inputs and associated values. For instance in PHP:

$total = $_POST['contacts'];
$contacts = array();

for( $i = 1; $i < $total; $i++ ) {
    $this_contact = $array(
        'Name'  => $_POST['name-' . $i],
        'Email' => $_POST['email-' . $i],
        'Phone' => $_POST['phone-' . $i]
    );
    array_push($contacts, $this_contact);
}

var_dump( $contacts );
like image 20
Lionel Ritchie the Manatee Avatar answered Oct 10 '22 16:10

Lionel Ritchie the Manatee