Vector sum doubt and bank recording

Asked

Viewed 52 times

0

I have this simple form

<form>
    <div class="itenspedido">
                <h4>Itens do Pedido</h4>
                    <div id="itens">
                                    <div class="div-bloco-form">
                                        <label class="obrigatorio">Quantidade:</label>
                                        <input type="text" name="$Quantidade"/>
                                    </div>
                    </div>
           </div>
 </form>   

here the form will be cloned

<script>
                    function adicionar() {
                        var itm = document.getElementById("itens").getElementsByClassName("div-bloco-form")[0];
                        var cln = itm.cloneNode(true);
                        var inputs = cln.getElementsByTagName("input");

                        for (i = 0; i <= inputs.length - 1; i++) {
                            if (inputs[i].type === "text")
                                inputs[i].value = "";
                        }

                        document.getElementById("itens").appendChild(cln);
                    }

                    function remover(btn) {
                        var div = btn.parentNode;

                        if (numLinhas() > 1)
                            div.remove();
                    }

                    function numLinhas() {
                        var linhas = document.getElementById("itens").getElementsByClassName("div-bloco-form");
                        return linhas.length;
                    }

                    </script>

Now the following question, as I will click on the button to clone the form. I would like at the end to record in the database, all Forms. I understand the logic that will be from 1 to N. However the use of is or foreach I believe that this is what I need to use I still don’t know how to do. I am a beginner, I need a help and I thank you from now on.

1 answer

0

Good by what is in your code today the function clones the quantity field within the form, to be included several different values in this field, it does not make much sense that having several quantity inputs (is not cloning the form) but if that’s what you really want it’s just you changing the input name to an array like this <input type="text" name="Quantidade[]"/> and retrieve values per post (if your form will be passed by post <form method="post">) testing with json_encode($_POST['Quantidade']), you will have the values in array ex: ["3","4"] where values 3 and 4 were included in the two inputs created in the form. and to retrieve and insert each value:

   while (list ($chave, $valor) = each ($_POST))    {
        if ($chave == "Quantidade") {
            foreach ($valor as $value) {
                //insert que será criado onde $value contém o valor digitado em cada input
            }
        }
    }

or you can:

in its add function put inside the for, in addition to creating with null value the inputs, change the name by adding numbers in front so inputs[i].name =inputs[i].name + numLinhas(); and to pick up later on the post you can use a for picking up values from the posts like this $_POST['Quantidade'.$i] where the $i would be the cloned fields 1, 2 and so on

  • It is that in the case I wanted to summarize the form so as not to stay a long code. It contains the product name, quantity, unit value and total.

  • I got edited my answer by adding code that I could use to change the name of the fields adding numbers to recover in the post within a for with the amount of cloned records.

  • for (inputs[i].name = 0; inputs[i].name + numLines();) { if (inputs[i].type === "text") inputs[i]. value = ""; } would be that?

  • for (i = 0; i <= inputs.length - 1; i++) { if (inputs[i].type === "text") inputs[i]. value = "";inputs[i]. name =inputs[i]. name + numinLines(); }

  • is the same for and if that you already have only add in if beyond inputs[i]. value="" also inputs[i]. name = inputs[i]. name + numLine();

  • Thanks for the help @Solange, I’ll try that then.

Show 1 more comment

Browser other questions tagged

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