Doubt of vector sum and recording in bank

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 in the end to write to the bank, all forms. I understand the logic that will be from 1 to N. However the use of for or foreach that I believe this is what I need to use I still do not know how to do. I'm a beginner, I need some help and thank you right away.

    
asked by anonymous 21.05.2017 / 01:24

1 answer

0

Good for what's in your code today the function clones the quantity field inside the form, to be included several different values in that field, it does not make much sense to have multiple quantity inputs (not cloning the form) but if it is so you just want to change the input name to an array like <input type="text" name="Quantidade[]"/> and retrieve the values per post (if your form is going to be passed <form method="post"> ) by 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 enter 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 your add function place in the for, besides creating with null the inputs, change the name adding numbers in front so inputs[i].name =inputs[i].name + numLinhas(); and to get later in the post you can use a for getting values of the posts so $_POST['Quantidade'.$i] where $ i would be the cloned fields 1, 2, and so on

    
21.05.2017 / 18:47