2013-06-24 71 views
3

我的jquery ajax调用正在更新我的数据库,但它没有更新页面。我不是明确了成功的参数,请帮助jquery ajax调用成功运行但页面没有更新

的jQuery:

<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> 

PHP:

<?php 
    if(isset($_GET['userId']) && isset($_GET['userStatus'])) { 
     $userId = $_GET['userId']; 
     $userStatus = $_GET['userStatus']; 
     switch($userStatus) { 
     case "1": 
      $changeStatus=0; 
      break; 
     case "0": 
      $changeStatus=1; 
      break; 
     default: 
      $changeStatus=""; 
    } 
    $Query = "UPDATE blog_users SET blog_user_status='$changeStatus' WHERE blog_user_id='$userId'"; 
    $Result = mysql_query($Query); 
} 
?> 

这是用户网是如何显示网页:

function viewUsers() { 
$Query="SELECT * FROM blog_users"; 
$Result=mysql_query($Query); 
while ($row=mysql_fetch_array($Result)) { 
    $userId=$row['blog_user_id']; 
    $userName=$row['blog_user_name']; 
    $userEmail=$row['blog_user_email']; 
    $userStatus=$row['blog_user_status']; 
?> 
<tr><td><?php echo $userId; ?></td> 
<td><?php echo $userName; ?></td> 
<td><?php echo $userEmail; ?></td> 
<td><?php if($userStatus==1){echo "Active";}else echo "Banned"; ?></td> 
<?php 
$action=""; 
switch($userStatus){ 
    case "1": 
     $action="Ban User"; 

     break; 
    case "0": 
     $action="Activate User"; 
     break; 
    default: 
     $action=""; 
} 
?> 
<td><a href="#" onclick="changeStatus(<?php echo $userStatus; ?>,<?php echo $userId; ?>);"><?php echo $action; ?></a></td> 
<?php } } ?> 

这就是我所说的功能“

<td><a href="#" onclick="changeStatus(<?php echo $userStatus; ?>,<?php echo $userId; ?>);"><?php echo $action; ?></a></td> 

和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> 

在实际的$action变量应改为[启用]点击用户或用户班恩!

+0

成功的jquery.ajax是执行一个Ajax的查询与成功解析的功能(因此,没有发生错误)。这是放置更改页面的代码的地方。你的成功功能是空的,因此,什么都不会改变。 – PerfectPixel

+0

是的,我试过把 成功:function(data){(“。tablesorter”)。html(data); } 但它只是把整个页面放在我的div –

+0

希望没有黑客可以做'?userStatus = 0&userId = 1'或'1'='1' –

回答

2

你必须记住,jQuery和PHP是完全独立的,只是更新数据库中的数据不会更新页面上的数据,直到刷新为止,因为PHP代码只会在页面第一次加载,而不是之后。你需要做的是在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?

如果您是,那么您需要从成功方法中生成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 
       }) 
     ) 
    ) 
) 
}) 

该段假定您的用户对象具有的属性名称,用户id,userStatus和动作,以及用户的反应是简单的用户对象的数组。

显然你要建立在你想要的格式的HTML,这只会产生

<tr> 
    <td> 
     <a href="#" onclick="updateStatus(status, id);">Action here</a> 
    </td> 
</tr> 

但它给你的,它会如何工作

+0

请看我的udpated代码..我粘贴了viewUsers函数调用我的页面来显示网格 –

+0

实际上bloguser.php有一个包含myfunctions.php的文件..所以所有的php代码都是为了功能页面而来的。 。bloguser只有标记和我的ajax调用 –

+0

好的。你真正需要的是一个不同的php文件,它只是从数据库中检索用户,并且只返回数据。因此,像blogUserJson.php(虽然我会选择一个更好的名称)并在那里,为用户做数据库查询,将每个用户放在一个数组中,然后将其序列化为JSON并返回。 – PaReeOhNos

0

你有一个粗略的想法几乎完成了所有的代码。我相信,在通过onclick事件更新表之前,您的代码是用来显示更新数据的php代码。

尝试重新加载页面,如:

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';  
} 
相关问题