Look at that:
- Insert several
<input>
unadjusted name
, they won’t even be submitted.
- Insert several
<input>
with the same value in the attribute name
, only the last will be submitted.
What you are wanting to do deserves a little more care, it is not simply insert a <input>
behind each other. In fact, it may be, since you do not need to recover this data later and I believe that the goal is not this.
What you can do is:
- Have several
<input>
with the attribute name
being an array. On the server you by iterate over that array and retrieve the data. For example:
<input type='text' name='empresas[]' placeholder='Nome da empresa 1'/>
<input type='text' name='empresas[]' placeholder='Nome da empresa 2'/> <!-- s/ problemas -->
Or else...
- Set the value of the attribute
name
being empresa-x
, where x
is the "field index" and this number should be incremented each time a new input
is entered in the form. In this case you must clone the last element, increment the value of x
and insert at the end of the form.
<input type='text' name='empresa-1' placeholder='Nome da empresa 1'/>
<input type='text' name='empresa-2' placeholder='Nome da empresa 2'/>
<!-- ... -->
The first is simpler, because all you have to do is clone the element. The second is a bit more boring to do because it is necessary to increase the index number and replace all attributes name
, but nothing complicated. It will depend on how you want to get this data on the server later.
Cloning with Array
$(function(){
$('button').on('click', function(){
$('.info-block:first').clone().insertAfter('.info-block:last');
});
});
.info-block { padding: 10px; border: 1px solid #ccc }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
<!-- bloco que será clonado -->
<div class='info-block'>
<input type='text' name='empresas[]' placeholder='Empresa'/>
<input type='text' name='escolas[]' placeholder='Escola'/>
<!-- outros campos... -->
</div>
</div>
<button>Adicionar + campos</button>
Cloning
In this second example, to make it easier to control the index I created a <span>
as if it were an accountant. Through it it is possible to obtain the last index to increment it and clone the block.
$(function(){
$('button').on('click', function(){
var $bloco = $('.info-block:last').clone(),
indice = parseInt($bloco.find('span').text()) + 1;
$bloco.find('span').text(indice);
$bloco.find('input[name^="empresa-"]').attr('name', 'empresa-' + indice);
$bloco.find('input[name^="escola-"]').attr('name', 'escola-' + indice);
// Se ouver outros inputs, altera o atributo name deles com o índice
$bloco.insertAfter('.info-block:last');
});
});
.info-block { padding: 10px; border: 1px solid #ccc }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
<!-- bloco que será clonado -->
<div class='info-block'>
<span>1</span>
<input type='text' name='empresa-1' placeholder='Empresa' />
<input type='text' name='escola-1' placeholder='Escola' />
<!-- outros campos... -->
</div>
</div>
<button>Adicionar + campos</button>
Show your code. What you’ve tried?
– Pablo Almeida
I haven’t tried anything yet, I’m early looking for something on the subject and did not find, so I decided to post here to the people try to help me. Like I said, I’m young, and I don’t have much logic.
– Guilherme SpinXO
You must have done something. Otherwise, you should start from the beginning. Also, why the Jquery tag? Why you assume that the solution to the problem is in Jquery?
– Pablo Almeida
Because I know that jQuery can use ajax not to reload the page. I’ve seen an example almost similar to what I want to do, but just brush and show.
– Guilherme SpinXO
Then you need to do AJAX? You should mention this in the question. And, yes, Jquery has facilities for AJAX, but it’s not required. Jquery is just a library. Why don’t you start by trying to learn how to do AJAX and, after you’re already getting the data (printing on the screen, for example), go back and ask a more specific question? It is difficult to answer such wide questions even if we try. It would not be an answer but a tutorial
– Pablo Almeida
Okay so guys, thanks for the info, I thought it was simple stuff and you would understand what I want, but all right, I’ll look for sources anything I create an answer later, or else I can develop a more specific question. Excuse me
– Guilherme SpinXO
@Guillermo does not need to apologize, only that he does not want to solve everything in one question. Go assembling your code, or seeing examples here on the site, and when you have a question about each of the steps, you can ask separately how to solve each one. The important thing is to make it happen step by step. We’re all here to help, but the questions better be very objective. Here are some cool tips: [Ask] and Community FAQ has some things that give a better idea of how the site works.
– Bacco
All right then, thank you @Bacco and Plabo
– Guilherme SpinXO
@See if this helps you: How to make a dynamic form?.
– Bacco
@Bacco, this will help me in something else, incredible, but that’s not what I wanted, but I ended up finding it here in Stack Over Flow, that’s what I intend to do: http://jsfiddle.net/MDL48/
– Guilherme SpinXO
@Great Guillermospinxo you found. Take a peek at the links I’ve recommended, on how to ask, so when you have questions and ask new questions, you can take better advantage of the site. The community will be easier to help if the question is more objective (and contains some code to be used as a basis). So you "help us help you" :)
– Bacco
Oops, all right @Bacco I’ll read it now.
– Guilherme SpinXO