2017-02-15 51 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