Dynamic Form? how to make?

0

I have the following question, I have a while in PHP, and in this while I have a form and a submit button that sends this form , but as they are multiple records it repeats this form and the system only detects the first form from the list, the others it does not do submit , for example, I have 20 records, 20 forms are generated, however much I change form # 18, it will save 01. >

To get clearer, each record brings me a status of the product in a select , and I want to leave this option of choice for the user, and when he chooses a status in that select (which is in form ), the system does the update on the database.

this is the code that while repeating how many results it finds:

<?php  
 while ($dados = $produtos->fetch_array()) {
 ?>
 <tbody> <tr>
    <td align="center"><?php echo $dados['id_oc']; ?></td>
    <td align="center"><?php echo date('d/m/Y', strtotime($dados['data']));  ?></td>
    <td align="center"><?php echo (number_format($dados['total'],0,",",".")); ?></td>
    <td align="center"><?php if(!empty($dados['faccao'])) echo $dados['faccao']; else     echo "-";  ?></td>
    <td align="center">
        <form action="" method="post" name="teste">
          <select name="status" id="status_">
          <option value="<?php echo $dados['status']; ?>"><?php echo $dados['status']; ?></option>
              <?php
              foreach ($arrEdital as $value => $name) {
                echo "<option value='{$name}'>{$name}</option>";
              }
              ?>
         </select>
         <td align="center"><input type="submit" name="status2" id="ok" value="ok" />    </td>
         </form>
    </td>    
  </tr></tbody>
<?php
} 
?>

The problem is that it repeats this 20 times (for example), but when I change the select from the number 18 (for example), it takes the values of the first form (number 1 in this example)

    
asked by anonymous 20.01.2015 / 02:26

2 answers

1

If you have 20 selects or inputs or whatever, you do not need 20 forms. Create only 1 form and place the fields within it. When the user submits the form, all values will be sent to the server.

Another thing, you're repeating name among the selects. You need a name different for each select or put it in array form by adding [] .

Your loop could look something like this.

<form action="" method="post">
    <?php while (...) { ?>
        <select name="teste[]">
            <option value="<?= $valorVindoBd ?>"><?= $valorVindoBd ?></option>
        </select>
    <?php } ?>
    <input typo"submit" value"ok" name="ok" />    
</form>

In this way, when submitting the form, your server will receive something like this.

$_POST["teste"] = array(
   0 => "opção selecionada no primeiro select",
   2 => "opção selecionada no segundo select",
   ...
   20 => "opção selecionada no último select",
);

I believe that using a name different for each select would be the best choice to give a meaning to the field.

    
20.01.2015 / 11:44
1

Denis, Oeslei is right. You do not need (and should not) create 1 form for each product. What you should do is create 1 status SELECT for each product within the same form and use an array notation in [name] or you can compose the field name with the product code, for example:

<select name="produto_<?php echo $dados[id_produto]; ?>">
...
</select>

To do this simply place the form tag before your products while:

    <?php
<form action="" method="post" name="teste">
     while ($dados = $produtos->fetch_array()) {
     ?>
     <tbody> <tr>
        <td align="center"><?php echo $dados['id_oc']; ?></td>
        <td align="center"><?php echo date('d/m/Y', strtotime($dados['data']));  ?></td>
        <td align="center"><?php echo (number_format($dados['total'],0,",",".")); ?></td>
        <td align="center"><?php if(!empty($dados['faccao'])) echo $dados['faccao']; else     echo "-";  ?></td>
        <td align="center">

              <select name="status[]" id="status_">
              <option value="<?php echo $dados['status']; ?>"><?php echo $dados['status']; ?></option>
                  <?php
                  foreach ($arrEdital as $value => $name) {
                    echo "<option value='{$name}'>{$name}</option>";
                  }
                  ?>
             </select>
             <td align="center"><input type="submit" name="status2" id="ok" value="ok" />    </td>

        </td>    
      </tr></tbody>
    <?php
    }
</form>
    ?>

I hope I have helped =)

    
20.01.2015 / 16:15