html能触发php函数吗?

html能触发php函数吗?

html不能触发php函数,html是超文本标记语言,只是用来构建页面结构的,不能触发php的某个函数。

要触发php的某个函数,我们有下面几个实现方法:

● 使用JavaScript来发送ajax请求,触发php的某个函数。

● 利用a标签,刷新页面,并带上一些参数来触发php的某个函数。

具体实现方式如下:

1、使用Ajax来触发php的函数

(1)JavaScript代码

1

2

3

4

5

6

7

8

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {

       if (this.readyState == 4 && this.status == 200) {

           console.log(this.responseText)

       }

};

xmlhttp.open("GET", "test.php?func=sayHi", true);

xmlhttp.send();

(2)test.php文件

1

2

3

4

5

6

7

8

9

<?php

    if ($_GET['func'] == 'sayHi') {

        sayHi();

    }

     

    function sayHi () {

        echo "hello world"

    }

?>

2、利用a标签来触发php的函数

(1)HTML代码

1

<a href="http://localhost/test.php?func=sayHi">触发php函数</a>

(2)合肥代孕威信15023219993太原代孕威信15023219993test.php文件同上

猜你喜欢

转载自www.cnblogs.com/ipengrui1/p/12283221.html