2012-06-09 13 views
3

當我嘗試使用jQuery發送var時,ajax php沒有收到它,我不知道爲什麼。我如何發送一個jQuery var到php?

這是我的腳本:

function showHiddenUserInfoPanel(id){ 
$(document).ready(function(){ 
    $('#admin-resume').fadeToggle('slow', 'linear', function(){ 
     $.ajax({ 
      async: true, 
      type: "POST", 
      dataType: "html", 
      contentType: "application/x-www-form-urlencoded", 
      data: "userid="+id, // Also I tried with : data: {userid: id}, 
      url: "user-profile.php", 
      success: function(){ 
       $('#info-user').fadeToggle('slow', 'linear'); 
       alert(id); // I receive the correct id value 
      } 
     }); 
    }); 
}); 

這是我的用戶profile.php:

<div id="activity_stats"> 
    <h3>Viendo el perfil de <?php echo $_POST["userid"]; ?></h3> 
</div> 

能有人能幫助我嗎?

謝謝:)

+0

使用Fiddler跟蹤HTTP請求並檢查是否正確... – CodeZombie

+0

爲什麼在函數內使用'$(document).ready()'?你希望等到文檔準備好之後再調用函數,但是在函數本身中它可能不會達到你期望的效果。這意味着你實際上並沒有在這個函數中做什麼,你只是綁定了一個事件。 – David

+0

@David好吧,如果DOM已經準備就緒,這個函數將立即被調用。 – lonesomeday

回答

7

難道只是我還是你不使用的響應呢?

$.ajax({ 
    success: function(){ 
    $('#info-user').fadeToggle('slow', 'linear'); 
    } 
}); 

您的匿名success函數忽略所有響應。我懷疑你的#info-user應該是一個空的元素,你沒有得到一個視覺反饋。

一個例子可以是:

$.ajax({ 
    success: function(response){ 
    $('#info-user').html(response).fadeToggle('slow', 'linear'); 
    } 
}); 

其它替代可以使用.load()

+0

爲了增加更多的信息,jQuery'.load()'函數可能對他的需求也很有用:http://api.jquery.com/load/ – David

+0

@David,我加了它;) – Alexander

+0

@Alexander對於load ()與ajax()相同,但只與數據,url和回調有關?謝謝:) – jask