Php jquery ajax调用已成功运行,但页面未更新
我的jqueryajax调用正在更新数据库,但没有更新页面。我不清楚成功参数,请帮助 jquery:Php jquery ajax调用已成功运行,但页面未更新,php,jquery,Php,Jquery,我的jqueryajax调用正在更新数据库,但没有更新页面。我不清楚成功参数,请帮助 jquery: <script type="text/javascript"> function changeStatus(userStatus,userId) { var getParameters = "userStatus = " + userStatus + "&userId = " + userId; $.ajax({ type: '
<script type="text/javascript">
function changeStatus(userStatus,userId) {
var getParameters = "userStatus = " + userStatus + "&userId = " + userId;
$.ajax({
type: 'GET',
url: 'blogUsers.php',
data: getParameters,
success: function() {
}
});
}
</script>
函数changeStatus(userStatus,userId){
var getParameters=“userStatus=“+userStatus+”&userId=“+userId;
$.ajax({
键入:“GET”,
url:'blogUsers.php',
数据:getParameters,
成功:函数(){
}
});
}
php:
这就是我如何调用函数“
thsi是需要更新的部分:
<table class="tablesorter" cellspacing="0">
<thead>
<tr>
<th class="header">userId</th>
<th class="header">userName</th>
<th class="header">userEmail</th>
<th class="header">userStatus</th>
<th class="header">Actions</th>
</tr>
</thead>
<tbody>
<?php viewUsers(); ?>
</tbody>
</table>
用户ID
用户名
用户电子邮件
用户状态
行动
实际上,
$action
变量应该更改为在单击时激活user或Bann user!您必须记住jQuery和PHP是完全独立的,在刷新之前,简单地更新数据库中的数据不会更新页面上的数据,因为PHP代码只会在页面第一次loa时运行您需要做的是在jQuery ajax调用中使用success
方法
$.ajax({
type:'GET',
url:'blogUsers.php',
data:getParameters,
success:function(data, status, jqxhr) {
... set users in view here ...
}
});
}
您从服务器返回数据的格式是什么?是blogUsers.php
返回HTML,还是返回JSON用户数组?如果是HTML,您只需将响应函数的主体设置为
$(".tablesorter tbody").html(data)
但我想你很可能会返回JSON
如果是,则需要从success方法中生成HTML。类似的方法应该可以工作:
$(".tablesorter tbody tr").remove();
$.each(data, function(user){
$(".tablesorter tbody").append(
$("<tr></tr>").append(
$("<td></td>").append(
$("<a></a>", {
href: "#",
onclick: "changeStatus(" + user.userStatus ", " + user.userId + ")",
text: user.action
})
)
)
)
})
$(“.tablesorter tbody tr”).remove();
$。每个(数据、功能(用户){
$(“.tablesorter tbody”).append(
$(“”)。附加(
$(“”)。附加(
$("
但它让您大致了解了它的工作方式您几乎已经完成了所有代码。我确信,php代码(显示更新数据的代码)是在通过onclick事件更新表之前执行的 尝试重新加载页面,如下所示:
success:function() { //you will not have to pass any parameter
on the function because you all done the show data in your `viewUsers()`.
//your page here, e.g window.location.href: 'bla2.php';
}
jquery.ajax中的Success是一个函数,在ajax查询成功解决后执行(因此,没有发生错误)。这是放置更改页面的代码的位置。Success函数为空,因此,不会有任何更改。是的,我尝试放置Success:function(data){$(“.tablesorter”).html(data);}但是它只是将整个页面重新放在我的分区中,希望没有黑客可以做
?userStatus=0&userId=1'或'1'='1
Lol,我只是知道这些天没有制作任何实时项目,所以现在我不必担心黑客可能尝试将数据记录到控制台,看看服务器实际返回的内容。然后你可以从当您缩小错误来源时,请查看我的udpated代码..我粘贴了在我的页面上调用的viewUsers函数,以显示GridFactory bloguser.php有一个包含myfunctions.php的文件..因此所有php代码都是针对函数页面的..bloguser只有标记和我的ajax callAh ok.那么您真正需要的是一个di不同的php文件,它只从数据库检索用户,只返回数据,所以类似于blogUserJson.php(尽管我会选择一个更好的名称)在那里,为用户执行数据库查询,将每个用户放入一个数组中,并将其序列化为JSON并返回。先生,我刚刚开始使用jquery和php,您告诉我的事情超出了我的理解:\n有没有简单的方法来更新$action?不幸的是没有。php和jquery是分开的。您的变量,例如$action ajQuery不知道它们是什么,也不知道关于它们的任何事情,因为当页面加载时PHP在服务器上运行,而jQuery在页面加载后在客户端计算机上运行。恐怕更新页面的唯一方法是让javascript更改它。
$(".tablesorter tbody").html(data)
$(".tablesorter tbody tr").remove();
$.each(data, function(user){
$(".tablesorter tbody").append(
$("<tr></tr>").append(
$("<td></td>").append(
$("<a></a>", {
href: "#",
onclick: "changeStatus(" + user.userStatus ", " + user.userId + ")",
text: user.action
})
)
)
)
})
<tr>
<td>
<a href="#" onclick="updateStatus(status, id);">Action here</a>
</td>
</tr>
success:function() { //you will not have to pass any parameter
on the function because you all done the show data in your `viewUsers()`.
//your page here, e.g window.location.href: 'bla2.php';
}