2013-06-24 70 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';  
} 
相關問題