2010-08-13 70 views
2

調用PHP腳本現在我有這樣的:使用Ajax和jQuery

echo "<a href='misc/removeWallComment.php?id=" . 
    $displayWall['id'] . "&uID" . $displayWall['uID'] . "&BuID" . 
    $displayWall['BuID'] . "' title='ta bort inlägg'> 
    <span class='removeWallComment'></span> </a>"; 

它的鏈接,當您單擊刪除評論圖標。

現在,它去misc/removeWallComment.php和回聲「評論刪除」。但我想將其與我目前的網站進行整合,所以你不會去另一個頁面刪除這個紀念品。有了這個,我想到了使用一個AJAX調用removeWallComment.php。現在

,你的鏈接看到它需要三個變量,iduIDBuID,我想POST,不GET發送,使用戶無法看到地址欄的變量。成功時,它應該提醒你。

我該怎麼做?

回答

0

正是如此,這個問題就會有答案了:

var links = $('.removeWallComment').parent(); 

links.click(function(event) { 
    // Parse out the ids 
    var data = this.href.substring('?').split('&'); 

    var id = data[0].substring(data[0].indexOf('=') + 1); 
    var uid = data[1].substring(data[1].indexOf('=') + 1); 
    var BuID = data[2].substring(data[2].indexOf('=') + 1); 

    $.post('misc/removeWallComment.php', { 
     'id': id, 
     'uid': uid, 
     'BuID': BuID 
    }, function(data){ 
     // Success! 
     alert('OK'); 
    }); 

    event.preventDefault(); 
}); 

如果使用GETPOST不要緊,如果你正在使用AJAX你的用戶將永遠不會看到三個id小號在地址欄中。然而,這三個id必須以某種方式得到,所以在這種情況下,我們解析它們的值爲href(三個id必須必須存儲在某個地方,最好在元素本身,以便於檢索。 id爲安全起見,這不是最好的方法)。