Post in Form with Dynamic Fields

0

Hello. I'm having trouble submitting my data to a form. My problem is this:

My client has created a series of products in the database. And now he needs a form in which will appear all the quantities of the products created. In this form it will update the quantity of available products.

There is no limit to the creation of products. Then the form may have more or less fields to fill in tomorrow.

I was able to generate the form as follows:

<?php
$produtos     = array();
$sql          = "select * from tb_produto";
$search_query = mysql_query($sql);

while($select = mysql_fetch_array($search_query)){
    $id_produto = $select["id_produto"];
    $titulo = $select["titulo"];
    $quantidade = $select["quantidade"];
    array_push($produtos, $titulo);
    ?>

    <div>
        <label><?php echo $titulo; ?></label>
        <input type="text" name="txtQuantidade" value="<?php echo $quantidade; ?>">
    </div>
    <?php

}
?>

Now I need to submit this data. My question is: how is it possible to submit a form consisting of an unknown number of fields at once?

    
asked by anonymous 05.05.2017 / 03:18

1 answer

1

You can pass the product id in name this way:

 <input type="text" name="txtQuantidade<?php echo $id_produto; ?>" value="<?php echo $quantidade; ?>">

and give post of the form, retrieving the id and value of it to give update this way:

while (list ($chave, $valor) = each ($_POST))   {
    reset ($_POST);
    if (substr($chave,0,13) == "txtQuantidade") {
        $guardaid = substr($chave,13);
        $sql_alteracao = "update tb_produto set quantidade = ".$_POST['txtQuantidade'.$guardaid]." where id_produto = ".$guardaid;

    }
}
    
05.05.2017 / 03:39