2017-02-15 40 views
0

我想在用戶確認後執行一些php代碼。我的意思是如果用戶點擊是的一些PHP函數應該執行。我該怎麼做?bootbox後如何使用php代碼確認提醒

bootbox.confirm({ 
 
    message: "This is a confirm with custom button text and color! Do you like it?", 
 
    buttons: { 
 
     confirm: { 
 
      label: 'Yes', 
 
      className: 'btn-success' 
 
     }, 
 
     cancel: { 
 
      label: 'No', 
 
      className: 'btn-danger' 
 
     } 
 
    }, 
 
    callback: function (result) { 
 
     console.log('This was logged in the callback: ' + result); 
 
    } 
 
});

+2

在回調函數中,您將向運行代碼的PHP頁面發出AJAX請求。 – David

+0

已經有回撥塊了;你可以使用alert來檢查按鈕的值:'callback:function(result){alert(result);}' – bharat

+0

在回調函數中,我該如何編寫php代碼? – rivas

回答

0

PHP代碼被執行服務器端(客戶端看到的網頁之前)和PHP代碼被刪除什麼是outputed給用戶。
要執行一些PHP代碼,您需要向服務器發出另一個請求。爲此,您可以使用XMLHTTPRequests或Ajax。

+0

你可以給我一個例子 – rivas

+1

@ user7393973給你一個XMLHttpRequest的好例子,如果你想:) – Ad5001

0

正如Ad5001 Gameur codeur autre所述,您有請求。這裏有一個例子:用戶點擊是

<script type="text/javascript"> 
function loadXMLDoc() { 
    var xmlhttp = new XMLHttpRequest(); 

    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == XMLHttpRequest.DONE) { 
      if (xmlhttp.status == 200) { 
       document.getElementById("myDiv").innerHTML = xmlhttp.responseText; 
      } 
      else if (xmlhttp.status == 400) { 
       alert('There was an error 400'); 
      } 
      else { 
       alert('something else other than 200 was returned'); 
      } 
     } 
    }; 

    xmlhttp.open("GET", "ajax_info.txt", true); 
    xmlhttp.send(); 
} 
</script> 

後,調用函數(在這種情況下,所謂的loadXMLDoc())。

不要忘記用您的PhP文件替換ajax_info.txt