This question already has an answer here:
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>
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'>");