在html中调用PHP函数

call a php function in html

本文关键字:PHP 函数 调用 html      更新时间:2023-09-26

假设我有一个名为"test.php"的php类,并且在该类中我有一个名为"my_test()"的函数。

现在我想调用这个函数当我的按钮被点击,所以在我的html类名为"my_page.html"写像这样——

<button name="mytestbutton" type="button" onclick="my_test()" class="btn btn-primary btn-xs" value="my_test()">My Test</button>

但是我需要写一些东西,所以它在我的html中识别onclick它应该做函数"my_test()"。但是我如何在html按钮中调用我的函数,有人能解决这个问题吗?

HTML, Javascript和您的按钮活动运行在您的客户端(您的web浏览器)。PHP在服务器上运行。向服务器发送信息的唯一方法是向服务器发送新页面请求、表单提交或所谓的AJAX请求,并让PHP代码获取已发送给它的数据

这些数据通常通过PHP的'superglobals' $_GET, $_POST, $_REQUEST等来获取。

或者你可以用JavaScript编写my_test并让它在浏览器上运行。

<script>
function my_test() {
   // your button code here
}
</script>
<!-- .. some HTML .... -->
<button name="mytestbutton" type="button" 
 onclick="my_test()" class="btn btn-primary btn-xs" value="my_test()">My Test</button>