delete from a row of the selected table

Asked

Viewed 315 times

1

I have the following div:

<div id="div_conteudo" align="center">
<div id="header">
<input type="button" name="Delete" class="botao1" style="float: right; margin-right:-12%">
</div>
<div id="foot" style="overflow: auto; width: 100%; height: 440px; border:solid 1px">
<section id="s1">
<div class="div1" id="minhaDiv" style="float: left;"> 
    <table id="table" class="table table-bordered">  
        <tr> 
            <th width="20%" style="text-align:center">De</th>
            <th width="60%" style="text-align:center">Assunto</th>
            <th width="10%" style="text-align:center">Prioridade</th>
            <th width="10%" style="text-align:center">Recebido</th>             
        </tr>
<?php  
    while($row = mysqli_fetch_array($result))  
        {  
?> 
        <tr>
        <th width="10%" colspan=4>Recebido: <?php echo $row["Data"]; ?></th>
        </tr>       

        <tr>  
        <td><?php echo $row["De"]; ?></td>
        <td class="td-info view_data" id="<?php echo $row["Id"]; ?>,<?php echo $row["Para"]; ?>" data-toggle="modal" href="#dataModal" width="20%" <?php echo $row["Status"] != '0'?' style="font-weight:bold" ':' style="font-weight:normal" '?>><?php echo $row["Assunto"]; ?></td>  
        <td><?php echo $row["Prioridade"]; ?></td> 
        <td><?php echo $row["Hora"]; ?></td>
        </tr>
<?php  
        }  
?> 
    </table> 

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

JavaScript to select line:

var tr = $('table tr:not(:first-child)');
tr.on('click', function () {
    var self = this;
    tr.each(function(){
        if(this == self) $(this).toggleClass('colorir');
        else $(this).removeClass('colorir');
    })
});

Within the div id="header" I have the button that will be to do delete of the table row, which is within the div id="foot". Now I can’t get the button to delete working.

  • I don’t understand... which line you want to delete by clicking the button?

  • @Jrd the line I want to delete is the one I select. According to the line I select, the button must delete

  • And this line is already being marked with the class "coloring", right?

  • @Jrd yes, is marked with that class

  • You want each row to have a delete button or a checkbox that you choose which records you want to delete?

  • @Joao Benthin , I don’t want a button on each line, I want that button I put up, which is inside the div header. But you can also have the option to select with a checkbox the ones I want to delete. See here in the image, select the line and then click the button link

  • @Bruno Should this record also be removed from the database? If yes, you already have a function for this or know how to do this removal?

  • @Jrd but if not remove from database, then when refresh page appears again or not?

  • @Bruno Yes, when update will return. The problem is that, in my opinion, your problem is very wide, we will have to develop the whole function for you... Have you tried to do this removal? Have you had any difficulty at all? If you have put your code, we can help you with the problems and even improve your code.

  • @Jrd the part of php I know how to do, the problem is when selecting the line as I inform the button that is that line that has to delete.

  • Please do not edit your question based on one or more answers. The questions here should be focused, ideally so that they can help other users. If so, ask a new question, and in it give the details of where you are.

  • @bfavaretto ok, thanks for the info, so I’ll remove that issue I asked the question

  • I’ve already done it :)

Show 8 more comments

2 answers

2


The idea is to go through all the tr which has the "color" class, capture its id and use the remove to remove from the table, from there vc makes the call for removal in PHP

function deletar(){
    var ids = []; //arraypara armazenar os id's a serem deletados
    $(".colorir").each(function(){ //percorre todos os tr que possui a classe colorir
        ids.push($(this).find("td.td-info view_data").attr("id")); //adiciona o id da linha ao array
        $(this).remove();
    })
    //só fazer a chamada para remoção das linhas no php
    console.log(ids);
}

Just call the function on its button

<input type="button" name="Delete" class="botao1" style="float: right; margin-right:-12%" onclick="deletar();">
  • I edited the question with the code, because when I call php by ajax, it is not sending Id, it always returns undefined. So do not remove in the database.

  • @Bruno Try sending with date: {ids: ids}

  • ajax continues without sending the id of td, always returns undefined

  • 1

    The problem was on this line ids.push($(this).find("td.td-info view_data").attr("id"));, so I switched to: ids.push($(this).find(".apagar").attr("Id")); and added the delete class to the td

1

I believe the solution can be done with PHP itself. This code will delete all lines that have checkbox marked.

<form method="POST" action="paginaDelete.php">
   <input type="submit" value="Apagar Linha"/>
   <table>
       <tr>
           <th>Nome</th>
           <th>Sobrenome</th>
       </tr>
       <?php
       while($row = $result->fetch_assoc()){
          echo" <tr>
              <td>".$row['nome']."</td>
              <td>".$row['sobrenome']."</td>
              <td><input type='checkbox' name='id[]' value='".$row['id']."' /></td>
           </tr>";
       } 
       ?>
   <table>         
</form>

On the page where delete is located you have to do the following

<?php
foreach($_POST['id'] as $id)
{
    $delete = "DELETE FROM tabela WHERE id='$id'";
    $mysqli->query($delete);
}
echo "
<script>
    alert('Linha deletada com sucesso!');
    window.location.href='../suapagina.php';
</script>";
?>
  • but in my project the delete button is in the header of the div, soon does not get inside the form

  • You can button outside the form using the tag form="idDoForm" on the submite button, as in the example below: <form id="myform" method="get" action="Something.php"> <type input="text" name="name" /> </form> <input type="Submit" form="myform" /> Or using javascript

  • yes you’re right, but I’ve already solved with the code that was above. I needed to call the function deletar in the onclick button

Browser other questions tagged

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