Delete mysql data by Jquery

Asked

Viewed 105 times

0

Colleagues,

I have an excerpt of the code where when clicking the button Add fields, I can add and send to the database.

inserir a descrição da imagem aqui

In the visualization, I’m bringing the data in the following way:

<?php    
$sql = mysqli_query($this->conexao,"SELECT * FROM produtos WHERE IDProdutos = '".$idProdutos."';");
    $visualizar = "<table border=\"0\">";

    while($jmTamanhos = mysqli_fetch_object($sql)){  
       if($jmTamanhos->Tamanho != ""){
          $visualizar .=  "<tr class='linhas'>
                           <td  style=\"padding: 5px\"><input type=\"text\" name=\"Tamanho[]\" class=\"form-control\" placeholder=\"Tamanho\" value='".$jmTamanhos->Tamanho."'></td>
                           <td  style=\"padding: 5px\"><input type=\"text\" name=\"Estoque[]\" class=\"form-control pull-left\" placeholder=\"Estoque\" value='".$jmTamanhos->Estoque."'></td>
                           <td  style=\"padding: 5px\"><button type=\"button\" class=\"removerCampo btn btn-danger\" title=\"Remover linha\"><i class=\"fa fa-minus-square\" aria-hidden=\"true\"></i> Remover</button></td>
                       </tr>";       
       }
    }
  $visualizar .= "</table>";
    return $visualizar;  
    }
?>

So far so good. He returns to me:

inserir a descrição da imagem aqui

So far all right, but how would I do for that by clicking remove, direct to the deletion page? I tried with the code below, but I’m not getting it. Follow the code:

 <script type="text/javascript">
    $(function () {
      function removeCampo() {
        $(".removerCampo").unbind("click");
        $(".removerCampo").bind("click", function () {
           if($("tr.linhas").length > 1){
            $(this).parent().parent().remove();
           }
        });
      } 
      $(".adicionarCampo").click(function () {
        novoCampo = $("tr.linhas:first").clone();
        novoCampo.find("input").val("");
        novoCampo.insertAfter("tr.linhas:last");
        removeCampo();
      });


     $(".removerCampo").click(function () {
      $.post("excluir-tamanhos.php", $("#excluirCampos").serialize(), function(response) {
          alert('aqui 2');
        $('#success').html(response);
          removeCampo();    
      });
   return false;
  });

 });
</script>
  • what exactly isn’t working? the file excluir-tamanhos.php is receiving the data? Put it there... (The -1 was not I)

  • I changed it to $.post("delete-sizes.php", {Idproducts: 15, Size: 34}, Function(Response) { e worked. How do I pass product value 15 and size 34? when I put id='size' in name='Size[]' to work.

  • Hello @Localhost .. I can’t actually upload the data to the file delete-sizes.php. The problem is not in PHP but because I cannot send the data to that page.

  • I think I get it, and I think your mistake might be in the way that you’re sending the data, with the serialize. You can create the same function to remove the data and still remove from the database. Ai Voce can take the data that Voce will delete and send, instead of giving the serialize

  • Unfortunately I am a layman in jquery. You would have some example to show me?

  • I’ll see if I can set an example here

Show 1 more comment

1 answer

2


I made an example here friend, created in the same function to delete and send the data... Any doubt comments there.

<script type="text/javascript">
$( function () {

            $( ".removerCampo" ).on( "click", function () {
                if ( $( "tr.linhas" ).length > 1 ) {

                    alert( 'aqui 1' );
                    //aqui você esta removendo a linha na qual voce clicou em excluir
                    $( this ).parent().parent().remove();
                    //aqui voce busca dentro dessa linha o input com o nome tamanho
                    var $tamanho = $( this ).parent().parent().find( "input[name=\"Tamanho[]\"]" ).val();
                    //aqui voce busca dentro dessa linha o input com o nome tamanho
                    var $estoque =  $( this ).parent().parent().find( "input[name=\"Estoque[]\"]" ).val();
                        // agora envia para sua pagina de excluir
                        $.post( "excluir-tamanhos.php", {
                            IDProdutos: $estoque,
                            Tamanho: $tamanho
                        }, function ( response ) {
                            alert( 'aqui 2' );
                            $( '#success' ).html( response );
                        } );
                }
                    } );

                $( ".adicionarCampo" ).click( function () {
                    novoCampo = $( "tr.linhas:first" ).clone();
                    novoCampo.find( "input" ).val( "" );
                    novoCampo.insertAfter( "tr.linhas:last" );
                    removeCampo();
                } );

            } );

Vale Lembrar:

parent() : "Take" the parent element

find() : "Carries out a search", picking up the child element

  • Perfect Localhost. It worked! Thank you.

Browser other questions tagged

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