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'>");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?