Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
PHP/HTML Input Type Number

is there any way I can get the iterate through a lot of input type number's and only print out the values that have a value greater than 0?

Let's say I have 10

        <form action="displayInfo.php" method="post">


    <table cellpadding="6">
      <tr>
        <th>Description</th>
        <th>Price</th>
        <th>Weight</th>
        <th>Image</th> 
        <th>Quantity</th>
      </tr>
    <!-- PHP LANGUAGE -->
    <?PHP
        $sql = "SELECT * FROM parts";
        $q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorIndo()));

        while( $row = $q->fetch(PDO::FETCH_ASSOC))
        {

            echo '<tr>' .
                '<td>' . $row['description'] . '</td>' .
                '<td>' . $row['price'] . '</td>' .
                '<td>' . $row['weight'] . '</td>' . 
                '<td> <img src="' . $row[pictureURL] .'" alt="' . $row[number] . '" style="width:50px;height:50px;">' .
                '<td> <input type="number" name = "' . $row[number] ." id= "'. $row[number] . '"  value="0">' .
                '</td>' . '</tr>';
        }
        echo "</table>";

    ?> 
    </table> <br>



    <input type="button" id="submit" value="Submit" />
    </form>

So they are dynamically created with an id that have values 1,2,3,...10. The values are then updated through user input.

Is there anyway I can catch the data passed by the submit button for the values entered by the user that are greater than 0? If so, how would I also pass the description or even the $row[number] along with the value that is associated with it. the id of the input type number = the $row[number] as displayed in the code.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The simplest way is probably to use a PHP feature, explained at How do I create arrays in a HTML <form>?

In short, you rename the form controls to use a common prefix, add square brackets to make PHP convert them into array and then loop data as any other array:

'<td> <input type="number" name = "row[' . $row[number] . ']" id= "'. $row[number] . '"  value="0">' .

I've also fixed a missing quote but I presume it isn't in your actual code.

Then:

foreach ($_POST['row'] as $number => $value) {
}

And since it's an array you can use the usual goodies, e.g.:

$rows = array_filter($_POST['row'], function($val){
    return is_numeric($val) && $val>10;
});

Demo

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda