简体   繁体   中英

post values of inputs with same name

I'm having a problem passing values of inputs with same name, that was working but now tha just doesnt work anymore here is the html and the php

html:

<form action="alterado.php" method="post">
<p class="codigo"><input type="text" name="codigo-peca[]"></p>
<p class="descricao-peca"><input type="text" name="descricao-peca[]"></p>
<p class="valor-peca">R$<input type="text" name="valor-peca[]"></p>

<p class="codigo"><input type="text" name="codigo-peca[]"></p>
<p class="descricao-peca"><input type="text" name="descricao-peca[]"></p>
<p class="valor-peca">R$<input type="text" name="valor-peca[]"></p>

<input type="submit" value="Salvar Dados">

alterado.php

   if(is_array($_POST['valor-peca']) && is_array($_POST['codigo-peca']) && is_array($_POST['descricao-peca'])  ) {
    // ... code
    for($i = 0; $i < count($_POST['valor-peca']); $i++) {
        // ... reference index of arrays
        $valorPeca = $_POST['valor-peca'][$i];
        $codigoPeca = $_POST['codigo-peca'][$i];
        $descricaoPeca = $_POST['descricao-peca'][$i];

        if ($valorPeca != 0){
        $SQL = "INSERT INTO pecas (ordemServico, codigoPeca, descricaoPeca, valorPeca) VALUES ('$ordem', '$codigoPeca', '$descricaoPeca', '$valorPeca');";
$result = mysql_query($SQL);
}
        }
    }

but that is reading only the first input value but not the second

You are overwriting the the variables $valorPeca , $codigoPeca and $descricaoPeca . It will only show one variable at a time unless you append them together.

if(is_array($_POST['valor-peca']) && is_array($_POST['codigo-peca']) && is_array($_POST['descricao-peca'])  ) {
            $valorPeca = '';
            $codigoPeca = '';
            $descricaoPeca = ''
            for($i = 0; $i < count($_POST['valor-peca']); $i++) {
                // ... reference index of arrays
                $valorPeca .= $_POST['valor-peca'][$i] . " ";
                $codigoPeca .= $_POST['codigo-peca'][$i] . " ";
                $descricaoPeca .= $_POST['descricao-peca'][$i] . " ";
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM