简体   繁体   中英

Bootstrap Modal Dialog on submitted not displaying id value

I've a problem using a modal dialog of bootstrap. This is my code: I'd like displaying id button correctly not just open the modal dialog but it not works. It displaying always blank. But if I change type ' button ' with ' submit ' get correctly id but not displaying the modal dialog. What's the problem?? Thank's for the attention. This is my code:

<table class="table table-bordered">
    <tr>
      <td> <input type="checkbox" id="box1" name="box1"> <b>Seleziona tutto</b></td>
      <td> <b>Pratica</b> </td>
      <td> <b>Filiale</b> </td>
    </tr>
    <?php
      $query = mysql_query("SELECT * from plprat ") ;
      while ($row = mysql_fetch_array($query)) {
        echo "<tr>";
        echo "<td><input type='checkbox' class='box' name='box[]' value=".$row['id']."></td>";
        echo "<td>" . $row['pratica'] . "</td>";
        echo "<td>" . $row['filiale'] . "</td>";
        //echo "<td><form method='POST' action='Cancella.php'><button type='submit' name='Cancella' class='btn btn-default' value=".$row['id'].">Cancella</button></form></td>";
        echo "<td><form method='GET'><button type='button' id='modifica' name='modifica' data-toggle='modal' data-target='#myModal' class='btn btn-default' value=".$row['id'].">Modifica</button></form></td>";
        echo "</tr>";
      }
    ?>
  </table>
<!-- Modal -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Modifica dati</h4>
        </div>

          <div class="modal-body">
            <?php
              $id = $_GET['modifica'];
              echo $id;
              $query = mysql_query("SELECT * FROM plprat WHERE id = $id");
              while ($row = mysql_fetch_array($query)) {
            ?>
                <b>Pratica</b> <input type="text" id="pratica" name="pratica" value="<?php echo $row['pratica']; ?>">
                <b>Filiale</b> <input type="text" id="filiale" name="filiale" value="<?php echo $row['filiale']; ?>">
            <?php
              }
            ?>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
            <button type="submit" id="salva" name="salva" class="btn btn-primary">Modifica</button>
          </div>

      </div>
    </div>
  </div>

This behavior is what you coded, let's me explain what is happening.

Why is this not working ?

You put your modal button in a form but you don't use a form in your modal. So if you change your modal button to a submit, it will submit it when you click it but your modal is not opening and even... the page is already submitting a form.
Your modal does not contain any <form> , so your Save button is not in a <form> and will never submit anything.

How to fix that ?

First remove the form tag wrapping your modal button, it's useless. Put a form tag directly as child of .modal-content and put the input data you need in it.

How could I help you more ?

  • In PHP, use only one echo to display multiple lines, you are making slower using too much echo for no reason.
  • Prefer to code in English, avoid your own language (if not English obviously), you will make it more accessible to other and you will make it more valuable.
  • Fill your modal dynamically.
  • You may need this : JSON data into a Bootstrap modal
  • Use a PHP framework or at least a library for your DBMS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM