[英]Data will not refresh after AJAX call
I have a HTML table with three columns 我有一个包含三列的HTML表
ID, BuildingLocation, Status
and a Active Link
in each row. 在每一行中都有一个
Active Link
。 When I Click on Active Link, Status value changed from 0 to 1 into the database but updated value data from the database is not displayed into the HTML table. 当我单击活动链接时,状态值从0更改为1进入数据库,但数据库中的更新值数据未显示在HTML表中。 It will display when I press the F5 key.
当我按F5键时,它将显示。
Building.php 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 Active(ID)
{
$.ajax(
{
type: "POST",
url: "buildingactive.php",
data: {ID:ID},
dataType: "JSON",
success: function(data)
{
$("#Response").html(data);
},
error: function(err)
{
//console.log("Fail"+err.call);
$("#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>");
echo("<td>".$Row["ID"]."</td>");
echo("<td>".$Row["BuildingLocation"]."</td>");
echo("<td>".$Row["Status"]."</td>");
echo("<td>");
echo("<a href='#' onclick='Active($ID)'>Change</a>");
echo("</td>");
echo("</tr>");
}
?>
</table>
<?php
echo("<div>");
echo("<p id='Response'></p>");
echo("</div>");
?>
</body>
</html>
buildingactive.php buildingactive.php
This is my PHP file which is used to update the status column of the buildingmaster table. 这是我的PHP文件,用于更新buildingmaster表的状态列。
<?php
$ID=$_POST['ID'];
$UpdateQuery="Update buildingmaster set Status=1 where ID=$ID";
require_once "connection.php";
$R=mysqli_query($CN,$UpdateQuery);
if($R==1)
{
$res="Building Active Successfully:";
echo json_encode($res);
}
else
{
$error="Server Error... Try Again...";
echo json_encode($error);
}
?>
Add to success: function(data){}
success: function(data){}
either window.location.reload();
要么
window.location.reload();
or you can assign 或者你可以分配
window.location = window.location
You have to pass status value along with success message like, 您必须传递状态值以及成功消息,例如,
$res={status:'1', msg: 'Building Active Successfully:'};
And you have to decode the data in Ajax success 而且您必须成功解码Ajax中的数据
var myObj = $.parseJSON(data);
Modified Code, 修改后的代码
<!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 Active(ID)
{
$.ajax(
{
type: "POST",
url: "buildingactive.php",
data: {ID:ID},
dataType: "JSON",
success: function(data)
{
$("#Response").html(data.msg);
$(".row"+ID).text(data.staus);
$("#Response").addClass("alert alert-success");
$("#Response").fadeOut(3000);
},
error: function(err)
{
//console.log("Fail"+err.call);
$("#Response").html(err);
$("#Response").addClass("alert alert-danger");
$("#Response").fadeOut(3000);
}
});
}
</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>");
echo("<td class='row".$ID."'>".$Row["ID"]."</td>");
echo("<td>".$Row["BuildingLocation"]."</td>");
echo("<td>".$Row["StatusName"]."</td>");
echo("<td>");
echo("<a href='#' onclick='Active($ID)'>Change</i></a>");
echo("</td>");
echo("</tr>");
}
?>
</table>
<?php
echo("<div>");
echo("<p id='Response'></p>");
echo("</div>");
?>
</body>
</html>
<?php
$ID=$_POST['ID'];
$UpdateQuery="Update buildingmaster set Status=1 where ID=$ID";
require_once "connection.php";
$R=mysqli_query($CN,$UpdateQuery);
if($R==1)
{
$res={status:'1', msg: 'Building Active Successfully:'};
echo json_encode($res);
}
else
{
$error="Server Error... Try Again...";
echo json_encode($error);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.