点击 HTML 链接触发 PHP 功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8662535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:12:23  来源:igfitidea点击:

Trigger PHP function by clicking HTML link

phphtmlfunction

提问by fart-y-goer

Is it possible to trigger a PHP function by just clicking a link? Or should I stick with the form submit method? If clicking the link will work, where should the link refer to?

是否可以通过单击链接来触发 PHP 函数?或者我应该坚持使用表单提交方法?如果点击链接有效,链接应该指向哪里?

Here is a sample code:

这是一个示例代码:

<?php
    session_start();
    function listMe($username){
        $username = mysql_real_escape_string($username);
        $query = mysql_query("INSERT INTO List (Usernames) VALUES ('$username')") or die(mysql_error());
    }
?>

<html>
    <head>
        <title>SAMPLE</title>
    </head>
    <body>
        <a href="**__???___**">Add my username to the list</a>
        <?php 
            listMe($_SESSION['Username']); 
        ?>
    </body>
</html>

Maybe someone can point me in the right direction. Thanks!

也许有人可以指出我正确的方向。谢谢!

回答by Zack Zatkin-Gold

You can do this by means of loading the entire page over again by the use of form submission, or by loading specific page contents directly into the page without needing to go from page to page. The second method is called "AJAX" (Asynchoronous Javascript and XML). Here are two examples, one of each specified.

您可以通过使用表单提交重新加载整个页面来实现这一点,或者通过将特定页面内容直接加载到页面中而无需从页面到页面进行加载。第二种方法称为“AJAX”(异步 Javascript 和 XML)。这里有两个示例,每个示例都指定了一个。

Form submission approach

表单提交方式

form.php

表单.php

<?php
function get_users(){
}
if(isset($_GET['get_users']))
{
    get_users();
}
?>
...
<form method="get">
    <input type="hidden" name="get_users">
    <input type="submit">
</form>

AJAX approach

AJAX 方法

ajax_file.php

ajax_file.php

<?php
function call_me(){
    // your php code
}
call_me();
?>

form.html

表单.html

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            // do something if the page loaded successfully
        }
    }
    xmlhttp.open("GET","ajax_file.php",true);
    xmlhttp.send();
}
</script>
</head>
<body>
<a href="#" onclick="loadXMLDoc()">click to call function</a>
</body>
</html>

回答by gotofritz

You can pass it as a query parameter of the link. http://example.com/?command=listMe&username=tomHowever that way everybody will be able to run the function by loading that URL

您可以将其作为链接的查询参数传递。 http://example.com/?command=listMe&username=tom但是这样每个人都可以通过加载该 URL 来运行该函数

<a href="http://example.com/?command=listMe&">List me</a>

and in the PHP

并在 PHP 中

<?php
if( isset($_GET['list_me']) && isset($_SESSION['Username'] ) ){
   listMe( $_SESSION['Username'] );
}
?>

回答by powerbuoy

HTML

HTML

<a href="?list_me">list me</a>

PHP

PHP

<?php
if (isset($_GET['list_me'])) listMe();

回答by Dominic Green

To trigger a function on link click with php the only way I know would be to append a param in the url of the link and then listen for that

要在使用 php 的链接单击上触发函数,我知道的唯一方法是在链接的 url 中附加一个参数,然后监听它

<a href="?function">Add my username to the list</a>

Then check for link

然后检查链接

if (isset($_GET['function'])){
      runFunction();
}

This is because php is a server side technology if you want to fire something without refreshing the page you would need to look at something like javascript

这是因为 php 是一种服务器端技术,如果您想在不刷新页面的情况下触发某些内容,则需要查看诸如 javascript 之类的内容

回答by jilt

I found this code in a plugin, they have user a foreach look to trigger the action:

我在插件中找到了这段代码,他们让用户使用 foreach 外观来触发操作:

$actions = unset($meta[$key]);
foreach ( $actions as $action => $value ) {
    echo '<li><a href="' . esc_url( $value['url'] ) . '" class="my-action-' . $action . '">' . '<i class="fa fa-times"></i></a></li>';
}