While not validating last php array

-3

Does anyone know why the button in the last while record does not execute the form's action?

<?php
 while($dados_tabela = $consulta_tabela -> fetchObject()) {
?>
  <tr>
    <td><?php echo $dados_tabela->id; ?></td>
    <td><?php echo $dados_tabela->nome; ?></td>
    <td><?php echo $dados_tabela->usuario; ?></td>
    <td><?php echo $dados_tabela->senha; ?></td>
    <td><?php echo $dados_tabela->email; ?></td>
    <td align="center">
      <table>
        <tr>
          <td>
            <form name="editar_usuario" action="../php/editar_usuario.php" method="post">
              <input type="hidden" name="id_editar" value="<?php echo $dados_tabela->id; ?>" />
              <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-edit"></i></button>
            </form>
          </td>
          <td>
            <form name="excluir_usuario" action="../php/excluir_usuario.php" method="post">
              <input type="hidden" name="id_excluir" value="<?php echo $dados_tabela->id; ?>" />
              <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-trash-o"></i></button>
            </form>
          </td>
        </tr>
      </table>      
    </td>
  </tr>
<?php
}
?>  
    
asked by anonymous 30.06.2016 / 19:19

2 answers

0

Your problem can be solved in this way, put together the jquery library:

    <?php

     $url_editar = '../php/editar_usuario.php';
     $url_excluir = '../php/excluir_usuario.php';

     while ($dados_tabela = $consulta_tabela -> fetchObject()) {
      ?>
      <tr>
        <td><?php echo $dados_tabela->id; ?></td>
        <td><?php echo $dados_tabela->nome; ?></td>
        <td><?php echo $dados_tabela->usuario; ?></td>
        <td><?php echo $dados_tabela->senha; ?></td>
        <td><?php echo $dados_tabela->email; ?></td>
        <td align="center">
          <table>
            <tr>
              <td>
                <button type="button" id="editar_<?php echo $dados_tabela->id; ?>" class="btn btn-default btn-xs btn-edit" data-id="<?php echo $dados_tabela->id; ?>"><i class="fa fa-edit"></i></button>
              </td>
              <td>
               <button type="button" id="excluir_<?php echo $dados_tabela->id; ?>" class="btn btn-default btn-xs btn-delete" data-id="<?php echo $dados_tabela->id; ?>"><i class="fa fa-trash-o"></i></button>
              </td>
            </tr>
          </table>      
        </td>
      </tr>
      <?php
    }
    ?>
<form name="editar_usuario" action="<?php echo $url_editar?>" method="post">
 <input type="hidden" id="id_editar" name="id_editar" value="" />
</form>
<form name="excluir_usuario" action="<?php echo $url_excluir?>" method="post">
 <input type="hidden" id="id_excluir" name="id_excluir" value="" />
</form>
<script src="https://code.jquery.com/jquery-latest.js"></script><scripttype="text/javascript">
$(function() {

  $('.btn-edit').on('click', function(){
      var id = $(this).data('id');
       $('#id_editar').val(id);
       $('[name="editar_usuario"]').submit();

  });

 $('.btn-delete').on('click', function(){
      var id = $(this).data('id');
       $('#id_excluir').val(id);
       $('[name="excluir_usuario"]').submit();
  });

});
</script>
    
30.06.2016 / 19:36
-2

I think it would be better if you did it that way, because repeating <form> for each record is not good practice.

<?php
 while($dados_tabela = $consulta_tabela -> fetchObject()) {

  ?>
  <tr>
    <td><?php echo $dados_tabela->id; ?></td>
    <td><?php echo $dados_tabela->nome; ?></td>
    <td><?php echo $dados_tabela->usuario; ?></td>
    <td><?php echo $dados_tabela->senha; ?></td>
    <td><?php echo $dados_tabela->email; ?></td>
    <td align="center">
      <table>
        <tr>
          <td>
            <a href="../php/excluir_usuario.php?id_editar=<?php echo $dados_tabela->id; ?>">Editar</a>
          </td>
          <td>
            <a href="../php/excluir_usuario.php?id_excluir=<?php echo $dados_tabela->id; ?>">Excluir</a>
          </td>
        </tr>
      </table>      
    </td>
  </tr>
  <?php
}
?>  
    
30.06.2016 / 19:35