运行 当用户使用 Post 方法单击 Link 时的 PHP 函数

Run a PHP Function when User Clicks a Link Using the Post Method

运行 下面的 PHP 代码最干净的方法是什么,而不是 $_GET,而是使用 $_POST?

<?php
function run_my_function() {
    echo 'I just ran a PHP function!';
}
if (isset($_GET['run'])) {
    run_my_function();
}
?>
<html>
<body>
    <p>Hello there! <a href="?run">Run a PHP function.</a></p>
</body>
</html>

NO,您不能从锚标签发送 post 请求。你将不得不使用 <form>

使用 jquery 来做到这一点,检查这个例子

<?php
if (isset($_POST)) {
    function run_my_function() {
            echo 'I just ran a PHP function!';
}
if (isset($_GET['run'])) {
    run_my_function();
}
}

?>
<script>

    $(document).ready(function () {

        $('#run').click(function () {

                $.ajax({
                     url: '/',
                     type: 'POST',

                     success: function(response) {
                         alert('hiii')

                         return response;
                     }
                });
             });

    });
</script>

<body>
    <p>Hello there! <a id='run' href="javascript:;">Run a PHP function.</a></p>
</body>