dousu8767 2019-01-16 18:34
浏览 58
已采纳

AJAX成功后删除表行[重复]

I have database table BuildingMaster with three fields ID, BuildingLocation and Status. I have PHP page which display all building details like ID, Location ,Status in HTML table . In Every row there is remove hyperlink.

When I click on link related record has been removed from the database. but it is still displayed into the HTML table. When I refresh the web page then it will removed.

Building.PHP

<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">

function Remove(ID) {   

    $.ajax({
        type: "POST",
        url: "buildingremove.php",
        data: {ID:ID},
        dataType: "JSON",
        success: function(data) {
             $("#Row"+ID).remove();
             $("#Response").html(data.MSG);
        },
        error: function(err) {
             $("#Response").html(err);
        }
    });

}
</script>
</head>
<body>
<table>
    <tr>
        <th>ID</th>
        <th>Location</th>
        <th>Status</th>
        <th>Action</th>
    </tr>
    <?php
    $sq="Select * from buildingmaster";

    $Table=mysqli_query($CN,$sq);

    while ($Row=mysqli_fetch_array($Table)) {  
        $ID=$Row['ID'];

        echo("<tr id='Row'.$ID.'>");
        echo("<td>".$Row["ID"]."</td>");
        echo("<td>".$Row["BuildingLocation"]."</td>");
        echo("<td>".$Row["Status"]."</td>");

        echo("<td>");
        echo("<a href='#' onclick='Remove($ID)'>Delete</a>");
        echo("</td>");
        echo("</tr>");  
    }
    ?>
</table>
<?php                                   
echo("<div>");
echo("<p id='Response'></p>");
echo("</div>"); 
?>
</body>
</html>

buildingremove.php

<?php

require_once "../core/connection.php";

$ID=$_POST['ID'];

$DeleteQuery="Delete from buildingmaster where ID=$ID";

global $CN;
$R=mysqli_query($CN,$DeleteQuery);

if($R==1)
{   
    $MSG="Building has been remove successfully.";
    $res = array('ID'=>$ID,'MSG'=>$MSG);
    echo json_encode($res);
}
else 
{
    $MSG="Server Error... Try Again....";
    $error = array('ID'=>$ID,'MSG'=>$MSG);
    echo json_encode($error);
}

?>
</div>
  • 写回答

1条回答 默认 最新

  • doudao2407 2019-01-16 18:41
    关注

    The ajax is working, the database is updated but the remove is not working because code cannot find the desired row. The syntax to assign Row ID is malformed

    Replace this:

    echo("<tr id='Row'.$ID.'>");
    

    with

    echo("<tr id='Row".$ID."'>");
    

    Or simply, as Quasimodo suggested

    echo("<tr id='Row$ID'>");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题