2012-05-18 29 views
5

OK,真的不知道,如果這是可能的,但認爲我會問反正:)使用JavaScript確認對話框來進行調用PHP在後臺

我有一個包含一些信息和鏈接的HTML頁面。 鏈接調用JavaScript確認對話框。 如果用戶選擇OK,那麼我想調用PHP文件中的函數,但在後臺,即我不想更改頁面,重新加載或任何其他內容。

這可能嗎?

感謝

牛逼

回答

6

使用AJAX。下面使用jQuery的:

if(confirm('Are you sure?')) { 
    $.ajax({ 
     url: '/path/to/file.php', 
     data: 'url=encoded&query=string', // Can also be an object 
     success: function(output) { 
      // This function is called after the PHP file completes. 
      // The variable passed in is a string containing all output of 
      // your PHP script (i.e. echo/print) 
     } 
    }); 
} 

欲瞭解更多信息,請查閱jQuery的文檔AJAX。如果你不能使用jQuery,有很多tutorials使用本地JavaScript來製作AJAX請求。

如果您要將大量數據從PHP返回給您的成功函數,那麼將PHP數據作爲JSON encoded array返回給客戶端,這可能是值得的。

3
$('.LinkClass').click(function(){ 

    if(confirm('Is it OK?')) { 

    $.ajax({ 
     url: 'action.php', 
     type: 'POST', 
     data: 'data=value', // Can also be an object 
     success: function(data) { 
      // Do Nothing 
     }, 
     error: function(){ 
      alert('Error'); 
     } 
    }); 

    } 

});