While does not validate last php array

Asked

Viewed 59 times

-3

Does anyone know why the button of the last while record does not perform the form 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
}
?>  
  • You cannot place the same form within a loop, put the form name and the form id, as a key variable, concatenating with the names.

  • Ivan actually "can", but is completely NOT recommended by myriad haha problems

  • It may just that it will always overwrite the information.

  • @Ivanferrer , could pass me how this amendment would look?

  • Why don’t you use foreach?

  • I will present a better solution by avoiding this Forms loop... because this really is not a good practice.

  • @Ivanferrer thanks for the solution via javascript however now it did not work at all. i didn’t want to go through $_GET to not be visible in the URL but I will try to call $_GET in the url of an iframe or by include but there is no idea how to implement

  • i edited, it had a variable more "form". that was actually the name of the form.

  • 1

    It has how to put the part of the query code, and the output of a print_r( $consulta_tabela ); to evaluate? By the way, not related to the question, but you can have a good dry HTML later, reorganizing the table outside. A good test is to see if there is no other element of the page with z-index larger than the desired button, which you can not analyze just by looking at your code.

  • And remove the excluir_user and editar_user from the Forms, it has no use in this case, and generates repeated names.

  • The problem of doing this: when you submit the post request, the HTML document looks for the <form> header, each form is an array and each element of the form is another array... if you put the same name, it is the same as always taking the same array key, it will search for the attribute of that array and will not find it because it was in another form of the same key. And he doesn’t make that distinction And he doesn’t make that mixture.

  • exactly @Ivanferrer think I’ll put form name concatenated with the recovered line id in the query so it doesn’t get the same form name on all lines.

  • I did what I said but unsuccessfully too. in case in each row would be a form with different name. unsuccessfully

Show 8 more comments

2 answers

0


Your problem can be solved 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>
<script type="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>
  • The last button still does not work as image on the link http://www.prestoexpress.com.br/erro.jpg

  • He’s taking the click by the boot class?

  • Not exactly, when you click the button, it performs a function that captures the data contained in that button and sends it to the form, and then submits it, out of the loop, this is not ajax.

  • Ajax would be a requisition procedure.

  • Because you denied the question, explain the problem.

  • I didn’t understand @Ivan, I didn’t negativei, alias I’m a little new around here still learning the rules. It’s improving the code with the changes that you mentioned

  • Someone’s been giving up bullshit around

  • I already gave UP in your reply.

Show 3 more comments

-2

I think it would be better if you did it that way, because repeat the <form> for each record is not a 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
}
?>  

  • Yeah... I wanted to avoid the $_GET ... but I think it would be the only solution at the moment

  • I understand... Another alternative is to do via JS, does not roll?

  • I thought about putting $_GET in an iframe so it is not visible in the URL ... but I’m still thinking about how to make this move or even in a include . Aff

  • Some reason not to do via Ajax?

  • No reason Ademilson, only I’m thinking about how to create this function and how to call it from the other page.

  • The best way would be to make an ajax implementation. Absolutely. using $.post('request.php', {id:id, acao:'excluir'}, function(e){ var rtn = $.parseJSON(e); console.log(s.msg) }) like this or $.ajax({...});

  • good @Ivanferrer hadn’t thought about it. I thought about going by See what you think? I actually wanted to avoid javascript and $_GET

  • @Ezequieltavares you can use Javascript and send via post with ajax ;) Ajax would solve several problems like "update" unnecessary page in case of "delete"...

Show 3 more comments

Browser other questions tagged

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