Form Post with Dynamic Fields

Asked

Viewed 459 times

0

Hello. I’m having trouble submitting data from a form. My problem is this:

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

There is no limit to the creation of products. So it may be that tomorrow the form has more or less fields to be filled.

I managed 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 formed by an unknown number of fields at once?

1 answer

1


You can pass the product id in the name this way:

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

and post the form, retrieving its id and value to 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;

    }
}
  • Thank you! You helped me a lot!!! Unfortunately I didn’t have time to test the solution until today. But I just implemented something based on what you suggested and worked perfectly. Thanks!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.