将PHP脚本与HTML Bootstrap按钮相结合



我在 page.html上有一些bootstrap按钮:

<button type="button" class="btn">Basic</button>

和一些PHP脚本script.php

有什么方法可以组合它们,因此用户单击按钮,脚本执行并将结果传递回page.html

我已经尝试搜索这个问题 - 但是有建议使用Ajax进行此问题。但是我想使代码尽可能简单。

只需在<form>中写入button,然后在<form>的操作属性中指定所需的目标URL。

如果您有表格,可以使用此

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<form action="/script.php">
  <button type="submit" class="btn">Basic</button>
</form>

或将其用于基本

<a href="/script" class="btn">Basic</a>

您需要使用Ajax进行此操作。Ajax函数的示例:

function sendRequest(type, url, callback, async, params) {
    if (async !== false) async = true;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = callback;
    xhttp.open(type, url, async);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send(params);
}

这就是您可以调用它的方式:

sendRequest('POST', 'yoururl', function(response) {
    if (this.readyState === 4) {
        //Handle the response
    }
}, true);

最新更新