Clone Input text and pass the value of cloned input via POST

Asked

Viewed 1,380 times

3

I need to clone an Input text by clicking on a "+" button and then pass the value of this cloned field via POST to the other form data...

I’m using a Javascript function to make the clones

<script>
function mais(campo1, campo2) { 
    var nova = document.getElementById("aqui");
    var novadiv = document.createElement("div");
    var nomediv = "div";
    novadiv.innerHTML = "<div class='col-md-10 col-sm-10'><br><input type='text' name='"+
    campo1+"' class='form-control' placeholder='Tipo de serviço...'/></div><div class='"+
    "'col-md-2 col-sm-2'><br><input type='text' name='"+campo2+"' class='form-control' placeholder='R$'/></div>";
    nova.appendChild(novadiv); 
}
</script>

Sample images:

Input padrão

Input com clones

Now I need to know how to get the value of the cloned fields and pass via POST

My HTML is:

New Budget
<hr style="height:1px; border:none; color:#e9e9e9; background-color:#e9e9e9; margin-top: 5px; margin-bottom: 0px;"/>
<br>


<form method="post" action="gerarpdf.php" name='msgform'  target='_blank'>


<div class="col-md-3 col-sm-3">
    <label class="control-label mll">
        Nº do Orçamento                           
    </label>
    <input type="text" name="orcamento" class="form-control" placeholder="0000000" readonly="readonly"/>
</div>

<div class="col-md-3 col-sm-3">
    <label class="control-label mll">
        Data                 
    </label>
    <input type="text" name="data" class="form-control" placeholder="dd/MM/aaaa" value="<?php echo date('d/m/Y') ?>"/>
</div>

<div class="col-md-6 col-sm-6">
    <label class="control-label mll">
        Vendedor                            
    </label>

    <input type="text" name="vendedor" class="form-control" placeholder="Nome do vendedor..."/>
</div>

<div class="col-md-6 col-sm-6">
    <label class="control-label mll">
    <br>
        Cliente                             
    </label>

    <input type="text" name="cliente" class="form-control" placeholder="Nome do cliente..."/>
</div>

<div class="col-md-3 col-sm-3">
    <label class="control-label mll">
    <br>
        Contato                             
    </label>
    <input type="text" name="contato" class="form-control" placeholder="Nome do contato..."/>
</div>

<div class="col-md-3 col-sm-3">
    <label class="control-label mll">
    <br>
        Telefone                           
    </label>
    <input type="text" name="telefone" class="form-control" placeholder="(xx)-xxxx-xxxx"/>
</div>

<div class="col-md-12 col-sm-12">
<br>
<label class="control-label mll">
        Observações
    </label>
    <textarea rows="4" name="observacoes" class="form-control">
    </textarea>
</div>

<div id="servico">
<div class="col-md-10 col-sm-10">
    <label class="control-label mll">
    <br>
        Serviço                         
    </label>
    <input type="text" name="servico" class="form-control" placeholder="Tipo de serviço..."/>           
</div>

<div class="col-md-2 col-sm-2">
    <label class="control-label mll">
    <br>
        Valor                        
    </label>
    <input type="text" name="valor" class="form-control" placeholder="R$"/>         
</div>

<div id="aqui">
</div>

</div>

<div class="col-md-12 col-sm-12">
<br>
<input type="button" value="+" onclick="mais(servico.value, valor.value)"; class="btn btn-outlined btn-success"/>           
</div>



<div class="col-md-12 col-sm-12">
<br><br><br>
<center>
    <input type="button" value="Finalizar" onclick="fin()"; class="btn btn-outlined btn-primary"/>
    <a href="?page=orcamentos&op=all"><input type="button" value="Cancelar" class="btn btn-outlined btn-danger"/></a>
</center>
    <br><br>
</div>


</form>
  • 1

    Can you put all the HTML in that form? You’re sending by Submit or ajax?

  • @Sergio, I posted the code...

  • I added HTML to the question, you can delete the answer you gave. Should the new field you insert have the same value as the one already on the page? or you want the values of each field to reach PHP independently?

  • Thanks, that’s right I need the values to reach php independently...

1 answer

3


The part you want to clone is:

<div id="servico">
    <div class="col-md-10 col-sm-10">
        <label class="control-label mll">
            <br/>Serviço</label>
        <input type="text" name="servico" class="form-control" placeholder="Tipo de serviço..." />
    </div>
    <div class="col-md-2 col-sm-2">
        <label class="control-label mll">
            <br/>Valor</label>
        <input type="text" name="valor" class="form-control" placeholder="R$" />
    </div>
    <div id="aqui"></div>
</div>

To send inputs with the same name to the server you must have [] at the end of name for PHP to treat as array.

I suggest you use the page’s own HTML in this function to clone, and note that since each input must have its value, the function does not need parameters (ie, you should take it in HTML too to stay only onclick="mais()":

function mais() {
    var destino = document.getElementById("aqui");
    var novadiv = document.createElement("div");
    var conteudo = document.getElementById("servico");
    novadiv.innerHTML = conteudo.innerHTML;
    destino.appendChild(novadiv);
}

and then in HTML change to:

<div id="servico">
    <div class="col-md-10 col-sm-10">
        <label class="control-label mll">
            <br/>Serviço</label>
        <input type="text" name="servico[]" class="form-control" placeholder="Tipo de serviço..." />
    </div>
    <div class="col-md-2 col-sm-2">
        <label class="control-label mll">
            <br/>Valor</label>
        <input type="text" name="valor[]" class="form-control" placeholder="R$" />
    </div>
</div>
<div id="aqui"></div>

Notice I put the <div id="aqui"></div> out of <div id="servico"> because you don’t have to be there and be cloned too.

jsFiddle: http://jsfiddle.net/qf2Lf914/

  • Man, thank you so much for the help so efficient, just one more question...and now in PHP how do I separate these values from the array $servico = $_POST['servico']; $value = $_POST['value'];

  • 1

    @Charles then when you do $servico = $_POST['servico']; the services variable will have an array, I don’t know if you want to save one by one or as a string... but take a look at an example here: http://answall.com/q/56231/129

  • 1

    I finally made it! That example helped me. Thank you so much for your help, now I can sleep quietly kkk until next time, really thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.