调用AJAX后数据不会刷新

时间:2019-01-16 12:20:19

标签: php jquery ajax

我有一个包含三列的HTML表

ID, BuildingLocation, Status

,每行中有一个Active Link。当我单击活动链接时,状态值从0更改为1进入数据库,但数据库中的更新值数据未显示在HTML表中。当我按F5键时,它将显示。

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

这是我的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);
}

?>

2 个答案:

答案 0 :(得分:0)

添加到success: function(data){}

window.location.reload();或您可以 分配

window.location = window.location

答案 1 :(得分:0)

您必须将状态值和成功消息(如

)一起传递
$res={status:'1', msg: 'Building Active Successfully:'};

而且您必须成功解码Ajax中的数据

var myObj = $.parseJSON(data);

修改后的代码,

<!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);
}

?>