Home  >  Article  >  Backend Development  >  Can html trigger php functions?

Can html trigger php functions?

angryTom
angryTomOriginal
2020-01-27 21:30:222884browse

Can html trigger php functions?

Can html trigger php functions?

html cannot trigger php functions. HTML is a hypertext markup language, which is only used to build the page structure. It cannot trigger a certain function of php.

To trigger a certain function of PHP, we have the following implementation methods:

● Use JavaScript to send an ajax request and trigger a certain function of PHP.

● Use the a tag to refresh the page and bring some parameters to trigger a certain function of PHP.

The specific implementation method is as follows:

1. Use Ajax to trigger php functions

(1) JavaScript code

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 file

<?php
    if ($_GET[&#39;func&#39;] == &#39;sayHi&#39;) {
        sayHi();
    }
    
    function sayHi () {
        echo "hello world"
    }
?>

2. Use a tag to trigger php function

(1) HTML code

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

(2 ) test.php file is the same as above

Recommended: "PHP Tutorial"

The above is the detailed content of Can html trigger php functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Does easyui support php?Next article:Does easyui support php?