dongren9966 2016-01-09 10:07
浏览 123

单击html表删除列单元格从数据库中删除表行(td = x)

I have html table which displays columns-client name, staff name and matter whereas in my database i have id,date,client_name,staff,matter columns. well that's not the problem. i wanted to delete the rows on button click so i just added a column delete which contains button with id-"del", and yes this column does not exist in my database. now I want to delete the rows from my table as well as database on the corresponding button click using Jquery. how do I do that? This is what I have tried so far-

$(document).ready(function(){   

$('#tableresult').on('click', '#del', (function(){
    var row = $(this).attr('id');
    $(#tableresult).removeRow(row);
});

});

n my html-

<td class="delete_td"><button id="del" btn btn-danger>&times;</button></td>

this is my html table code-

<table class="footable" data-filter="#filter" id="tableresult">
                               <thead>

                                <th>Client name</th>
                                 <th>Staff name</th>
                                 <th>Matter</th>
                                 <th> Delete</th>
                              </thead>

 <?php
include('db.php');
$sql=mysql_query("select * from newdata");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$clientname=$row['client_name'];
$staff=$row['staff'];
$matter=$row['matter'];

?>
<tr id="<?php echo $id; ?>" class="edit_tr">

<td class="edit_td" >
<span id="client_<?php echo $id; ?>" class="text"><?php echo $clientname; ?></span>
<input type="text" value="<?php echo $clientname; ?>" class="editbox" id="client_input_<?php echo $id; ?>" /&gt;
</td>

<td class="edit_td">
<span id="staff_<?php echo $id; ?>" class="text"><?php echo $staff; ?></span> 
<input type="text" value="<?php echo $staff; ?>" class="editbox" id="staff_input_<?php echo $id; ?>"/>
</td>

<td class="edit_td">
<span id="matter_<?php echo $id; ?>" class="text"><?php echo $matter; ?></span> 
<input type="text" value="<?php echo $matter; ?>" class="editbox" id="matter_input_<?php echo $id; ?>"/>
</td>

<td class="delete_td"><button id="del" btn btn-danger>&times;</button></td>

</tr>



<?php
}
?>

</tbody>
  <tfoot class="hide-if-no-paging">
    <th>                       </th>
     <th>
        <div class="pagination pagination-centered"></div>
     </th>
    <th>             </th>
    <th>             </th>
  </tfoot>
</table>

this is my table_edit.js code-see if this has any relevance

$(document).ready(function()
{
$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#client_"+ID).hide();
$("#staff_"+ID).hide();
$("#matter_"+ID).hide();
$("#client_input_"+ID).show();
$("#staff_input_"+ID).show();
$("#matter_input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var client=$("#client_input_"+ID).val();
var staff=$("#staff_input_"+ID).val();
var matter=$("#matter_input_"+ID).val();
var dataString = 'id='+ ID +'&clientname='+client+'&staff='+staff+'&matter='+matter;
//$("#client_"+ID).html('<img src="load.gif" />'); // Loading image

if(client.length>0 && staff.length>0 && matter.length>0)
{

$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#client_"+ID).html(client);
$("#staff_"+ID).html(staff);
$("#matter_"+ID).html(matter);
}
});
}
else
{
 alert('enter something');
}

});

// Edit input box click action
$(".editbox").mouseup(function() 
{
return false
});

// Outside click action
$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});

});
  • 写回答

2条回答 默认 最新

  • dongxi9326 2016-01-09 10:17
    关注

    your js should be like

    $(document).ready(function () {
    
        $('#tableresult').on('click', '#del', (function () {
            var row = $(this).attr('id');
    
            $.ajax({
            type: "GET",
                    url: 'http://yoursite_url/delete.php',
                    data: {id: row },
                    success: function (result) {
                    $('#'+row).remove();
                }
            });
        });
    });
    

    and delete.php

    include 'config.php' // DB Connection
    $id = $_GET['id'];
    $sql = mysql_query("DELETE FROM TABLE_NAME WHERE id = '$id'");
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装