Dynamic form? how to do?

Asked

Viewed 2,308 times

0

I have the following question, I have one while in PHP, and in this while have a form and a button of Submit who sends this form, but as there are several records he repeats this form and the system detects only the first form on the list, the others he does not do the Submit, for example, I have 20 records, 20 are generated Forms, no matter how much I change the form nº 18, it will save the 01.

To be clear, each registration brings me a status of the product in a select, and I want to leave this choice option to the user, and when he chooses a status in that select (who is in the form), the system do the update at the bank.

this is the code while repeats 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 of the number 18 (for example), it takes the values of the first form (number 1 in this example)

  • 1

    20 Forms? You want to change 18 and it affects 18 instead of 1? Is that it? What have you done so far? I don’t understand pq 20, if I understood you have something equivalent to a grid, maybe Javascript and ajax could solve. Really can’t understand your question, try to be clearer please.

  • I changed the question, sorry if it wasn’t clear.

  • You can show where you’re putting the while? And also how are you getting this value on the server? It will help to understand your question

  • @Peoplee and Guilherme, I edited the question with the code I am using, I believe that now you can see the problem, the result of while repeats several Forms, but when I click on the Ubmit button it updates only the first form of the list generated by while

  • @Denisl.Murara as I said in the first comment, maybe Ajax is a better way out.

  • @Guilhermenascimento have some link to some tutorial I can send me?

Show 1 more comment

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 inside it. When the user submits the form, all values will be sent to the server.

Another thing, you’re repeating the name between the selects. You need a name different for each select or put it in array form 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>

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.

  • I modified the code in the question, but I’ll try what you said!

  • @Denisl.Murara It is worth remembering that the logic is still very strange, because you will only have one option by select. In answer I am trying to help you understand your problem, but you need to review your logic =)

  • I edited the question with the code I am using, I believe that now you can see the problem, the result of while repeats several Forms, but when I click on the Submit button it updates only the first form of the list generated by while

1

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

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

For this just put the form tag before your product 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’ve helped =)

  • Thank you Thomas, I will try this way! Thank you!

  • this way if I have 15 records it will return 15 correct Forms, but even if I only change one form, it will pass an array with the 15 fields, even if I just want to change 1.

  • In fact it will create only 1 form, with 15 selects for example, then you recover the values of these selects and write to the database. Even if you have changed only 1, no problem, you can update using the 15 values, because the values will be the same, and the fields you have changed will be updated in the database.

Browser other questions tagged

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