"Multiple fields contact form"
Bootstrap 3.2.0 Snippet by maxicms

<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="row"> <h2>Multiple fields contact form</h2> <div class="col-md-4"> <div class="contacts"> <label>Contacts:</label> <div class="form-group multiple-form-group input-group"> <div class="input-group-btn input-group-select"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> <span class="concept">Phone</span> <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu"> <li><a href="#phone">Phone</a></li> <li><a href="#fax">Fax</a></li> <li><a href="#skype">Skype</a></li> <li><a href="#email">E-mail</a></li> <li><a href="#www">Web</a></li> </ul> <input type="hidden" class="input-group-select-val" name="contacts['type'][]" value="phone"> </div> <input type="text" name="contacts['value'][]" class="form-control"> <span class="input-group-btn"> <button type="button" class="btn btn-success btn-add">+</button> </span> </div> </div> </div> </div> </div> </div>
(function ($) { $(function () { var addFormGroup = function (event) { event.preventDefault(); var $formGroup = $(this).closest('.form-group'); var $multipleFormGroup = $formGroup.closest('.multiple-form-group'); var $formGroupClone = $formGroup.clone(); $(this) .toggleClass('btn-success btn-add btn-danger btn-remove') .html('–'); $formGroupClone.find('input').val(''); $formGroupClone.find('.concept').text('Phone'); $formGroupClone.insertAfter($formGroup); var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last'); if ($multipleFormGroup.data('max') <= countFormGroup($multipleFormGroup)) { $lastFormGroupLast.find('.btn-add').attr('disabled', true); } }; var removeFormGroup = function (event) { event.preventDefault(); var $formGroup = $(this).closest('.form-group'); var $multipleFormGroup = $formGroup.closest('.multiple-form-group'); var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last'); if ($multipleFormGroup.data('max') >= countFormGroup($multipleFormGroup)) { $lastFormGroupLast.find('.btn-add').attr('disabled', false); } $formGroup.remove(); }; var selectFormGroup = function (event) { event.preventDefault(); var $selectGroup = $(this).closest('.input-group-select'); var param = $(this).attr("href").replace("#",""); var concept = $(this).text(); $selectGroup.find('.concept').text(concept); $selectGroup.find('.input-group-select-val').val(param); } var countFormGroup = function ($form) { return $form.find('.form-group').length; }; $(document).on('click', '.btn-add', addFormGroup); $(document).on('click', '.btn-remove', removeFormGroup); $(document).on('click', '.dropdown-menu a', selectFormGroup); }); })(jQuery);

Related: See More

Questions / Comments:

There is a problem with the JS code for data-max. I assume it wants to limit the maximum fields to something but fails. To fix this, replace

var countFormGroup = .... ;


var countFormGroup = function ($form) {
return $form.siblings('.form-group').andSelf().length;

also, replace

var $lastFormGroupLast = ... ;


var $lastFormGroupLast = $multipleFormGroup.siblings('.form-group:last').andSelf();

Also, add the data-max attribute to the .mutliple-form-group.

Rodney Pantonial () - 9 years ago - Reply 0

Great Job !!
Unfortunately I don't know javascript but I have two questions:

1. how can I put a limit to the number of fields to be created. I would like to limit it to e.g. 8
2. Ist there is a possibilty to delete "used option" from selection ? E.g. If phone was already provided "phone" label shouldn't be available if I add new fields.

Thank you.

quabs () - 9 years ago - Reply 0

Please check out similar snippets on this site, there are some that have the functionality you are talking about :)

maxsurguy () - 9 years ago - Reply 0

Thank you could't find any but I'm focusing on a different solution.
Just one question: What does this line says: <input type="hidden" class="input-group-select-val" name="contacts['type'][]" value="phone">
I'm trying to find out this line is acutually used for expected for design purpose .

quabs () - 9 years ago - Reply 0

Hi guys,
Recently I did use this snipped, thanks a lot for it!
I'd like to notice one issue in JS code, after line #16 you should paste


Otherwise newly created inputs comes with empty type. Also I did my own parser for php side, it is very simple and I'd like to share it


Thanks :)

Igor Vovk () - 9 years ago - Reply 0

Thanks for this!

maxsurguy () - 9 years ago - Reply 0

I apologize for my English, I write very bad. To work with an I use the function


Maxim Kostjukevich () - 9 years ago - Reply 0


maxsurguy () - 9 years ago - Reply 0

any clue on after serializing the form, how to access the values in php side?

Behzad () - 9 years ago - Reply 0

Check out the code pasted by @disqus_mTp6jubeuF:disqus above!

maxsurguy () - 9 years ago - Reply 0

I believe you can access the array to retrieve them. What framework/script do you use to retrieve input elements?

maxsurguy () - 9 years ago - Reply 0