Pick up input values

0

Hello, I would like to know how to get information from each input of my form, are inputs in html and others generated in JavaScript, I want to send the information by PHP backend to another page.

Code:     Requests          

<form method="POST" action="Exibe_Pedidos.php?valida=TRUE">
        Receita
        <input type="text" name="receita" placeholder="Ex: Pão de queijo">
    <table border=1 id="tabela">
    <tr><td colspan="6" style="text-align: center"><label>Pedidos:</label></td></tr>
    <tr>

    <div id="codProduto">
        <td>Cód. Produto:</td> 
        <td><input type="text" name="codProduto[]" placeholder="Cód. Produto"></td>
    </div>




    <div id="quantidade">
        <td><label>Quantidade:</label></td>
        <td><input type="text" name="razao[]" placeholder="Quantidade"></td>
    </div>




    <div id="desconto">
        <td><label>Desconto:</label></td>
        <td><input type="text" name="desconto[]" placeholder="Desconto"></td>
    </div>
        </tr>

    </table>

Add   Register

</form> 


<script>

const adicionar = document.getElementById("adicionar");


const codProduto = document.getElementById("codProduto");
const quantidade = document.getElementById("quantidade");
const desconto = document.getElementById("desconto");


adicionar.addEventListener("click", function (event) {
let campo = document.createElement("input");
let campoQuantidade = document.createElement("input");
let campoDesconto = document.createElement("input");

campo.name = "";
campo.name = "";
campo.placeholder = "";

campoQuantidade.placeholder = "";
campoDesconto.placeholder = "Desconto";

var table = document.getElementById("tabela");
var row = table.insertRow(2);
row.insertCell(0).innerHTML = "Cód. Produto:";
row.insertCell(1).innerHTML = '<input type="text" name="codProduto[]" placeholder="CodProduto"> ';
row.insertCell(2).innerHTML = "Quantidade";
row.insertCell(3).innerHTML = '<input type="text" name="razao[]" placeholder="Quantidade">';
row.insertCell(4).innerHTML = "Desconto";
row.insertCell(5).innerHTML = '<input type="text" name="desconto[]" placeholder="Desconto">';

});

</script>

    
asked by anonymous 07.05.2018 / 23:56

2 answers

0

Your code has an error in const adicionar because it does not exist in your HTML, this should be checked.

Focusing on the question itself, to access the value of input just use it:

const codProduto = document.getElementById("codProduto").value;
const quantidade = document.getElementById("quantidade").value;
const razao = document.getElementById("razao").value;
const desconto = document.getElementById("desconto").value;
const receita = document.getElementById("receita").value;

With value you get exactly the value of input , this after changing your HTML and placed an ID for each field, with the same name that I entered in getElementById .

You can also get the value with onChange , where by changing the value of input , you get the value inside event.target.value , where event is a required parameter of the function called in onChange .

Here is an example where this can be verified. Note that I removed the code that is in error. Since it is in <form> , the function will be called when executing a submit , that is, pressing the enter key.

function mostrar(event) {
  console.log(event.target.value);
}
<form method="POST" action="Exibe_Pedidos.php?valida=TRUE">
        Receita
        <input type="text" id="receita" name="receita" placeholder="Ex: Pão de queijo">
    <table border=1 id="tabela">
    <tr><td colspan="6" style="text-align: center"><label>Pedidos:</label></td></tr>
    <tr>

    <div id="codProduto">
        <td>Cód. Produto:</td> 
        <td><input type="text" onchange="mostrar(event)" id="codProduto" name="codProduto[]" placeholder="Cód. Produto"></td>
    </div>




    <div id="quantidade">
        <td><label>Quantidade:</label></td>
        <td><input type="text" id="razao" name="razao[]" placeholder="Quantidade"></td>
    </div>




    <div id="desconto">
        <td><label>Desconto:</label></td>
        <td><input type="text" id="desconto" name="desconto[]" placeholder="Desconto"></td>
    </div>
        </tr>

    </table>
</form> 
    
08.05.2018 / 00:42
0

As far as I understand, you want to get these inputs in PHP, so you can use the $ _POST variable to get the input values from the name. More or less like this:

$input1 = $_POST["name_do_input"];

Remembering that the index string has to be the name, not the id. So when creating these inputs dynamically, you have to put different names.

Soon after you create the fields, you can name them as follows:

campo.name = "campo";
campoQuantidade.name = "quantidade";
campoDesconto.name = "desconto";

And you can get the values in PHP:

$campo = $_POST["campo"];
$quantidade = $_POST["quantidade"];
$desconto = $_POST["desconto"];
    
08.05.2018 / 01:17