php HREF调用PHP函数并传递变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17540228/
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-25 16:03:55  来源:igfitidea点击:

HREF to call a PHP function and pass a variable?

phphtml

提问by Squirtle

Is it possible to create an HREFlink that calls a PHP function and passes a variable along with it?

是否可以创建一个HREF链接来调用 PHP 函数并同时传递一个变量?

<?php
function sample(){
    foreach ($json_output->object ){
        $name = "{$object->title}";
        $id = "{$object->id}";

        print "<a href='search($id)' >$name</a>";
    }
}

function search($id){
    //run a search via the id provide by the clicking of that particular name link
}
?>

回答by PlausibleSarge

You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.

您无需使用框架即可轻松完成此操作。默认情况下,a 之后的任何内容?在 URL 中是一个 GET 变量。

So for example, www.google.com/search.html?term=blah

例如,www.google.com/search.html?term=blah

Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".

将转到 www.google.com/search.html,并将传递值为“blah”的 GET 变量“term”。

Multiple variables can be separated with a &

多个变量可以用 & 分隔

So for example, www.google.com/search.html?term=blah&term2=cool

例如,www.google.com/search.html?term=blah&term2=cool

The GET method is independent of PHP, and is part of the HTTP specification.

GET 方法独立于 PHP,是 HTTP 规范的一部分。

PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.

PHP 通过自动创建超全局变量 $_GET[] 来轻松处理 GET 请求,其中每个数组索引是一个 GET 变量名称,数组索引的值是该变量的值。

Here is some demo code to show how this works:

下面是一些演示代码来展示它是如何工作的:

<?php
    //check if the get variable exists
    if (isset($_GET['search']))
    {
        search($_GET['search']);
    }

    function Search($res)
    {
        //real search code goes here
        echo $res;
    }


?>

<a href="?search=15">Search</a>

which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets

这将打印出 15,因为它是搜索的值,而我的搜索虚拟函数只是打印出它得到的任何结果

回答by jeff

The HTML output needs to look like

HTML 输出需要看起来像

<a href="php_file.php?id=1">anchor text</a>

Your function will need to output this information within that format.

您的函数将需要以该格式输出此信息。

回答by janenz00

No, you cannot do it directly. You can only link to a URL.

不,你不能直接这样做。您只能链接到一个 URL。

In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:

在这种情况下,您可以在查询字符串中传递函数名称和参数,然后在 PHP 中进行处理,如下所示:

  print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";

And, in the PHP code :

而且,在 PHP 代码中:

  if ($_GET['fn'] == "search")
     if (!empty($_GET['id']))
            search($id);

Make sure that you sanitize the GET parameters.

确保清理 GET 参数。

回答by Quentin

No, at least not directly.

不,至少不是直接的。

  1. You can link to a URL
  2. You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
  3. That URL can be handled by a PHP program
  4. That PHP program can call a function as the only thing it does
  5. You can pass data from $_GET['foo']to that function
  1. 您可以链接到一个 URL
  2. 您可以在该 URL 的查询字符串中包含数据 ( <a href="myProgram.php?foo=bar">)
  3. 该 URL 可以由 PHP 程序处理
  4. 该 PHP 程序可以调用函数作为它唯一的作用
  5. 您可以将数据从$_GET['foo']该函数传递

回答by Limon

Yes, you can do it. Example:

是的,你可以做到。例子:

From your view:

从你的角度来看:

    <p><a href="test/1" class="btn blue">Edit</a>

Where 1 is a parameter you want to send. It can be a data taken from an object too.

其中 1 是您要发送的参数。它也可以是从对象中获取的数据。

From your controller:

从您的控制器:

function test($id){
  #code...
}

回答by zzlalani

Simply do this

只需这样做

<?php
function sample(){
    foreach ($json_output->object ){
        $name = "{$object->title}";
        $id = "{$object->id}";

        print "<a href='?search=" . $id . "' > " . $name . "</a>";
    }
}

if (isset($_REQUEST['search'])) {
    search($_REQUEST['search']);
}

function search($id){
    //run a search via the id provide by the clicking of that particular name link
}
?>

Also make sure that your $json_outputis accessible with is the sample()function.
You can do it either way

还要确保您$json_outputsample()功能是可访问的。
你可以用任何一种方式

<?php
function sample(){
    global $json_output;
    // rest of the code
}
?>

or

或者

<?php
function sample($json_output){
    // rest of the code
} 
?>

回答by riksof-zeeshan

Set query string in your link's hrefwith the value and access it with $_GETor $_REQUEST

href使用值在链接中设置查询字符串并使用$_GET或访问它$_REQUEST

<?php
    if ( isset($_REQUEST['search']) ) {
        search( $_REQUEST['search'] );
    }

    function Search($res) {
        // search here
    }

    echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>

回答by Kylie

Yes, this is possible, but you need an MVC type structure, and .htaccessURL rewriting turned on as well.

是的,这是可能的,但是您需要一个 MVC 类型的结构,并且还需要.htaccess打开 URL 重写。

Here's some reading material to get you started in understanding what MVC is all about.

这里有一些阅读材料,可帮助您开始了解 MVC 的全部内容。

http://www.phpro.org/tutorials/Model-View-Controller-MVC.html

And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

如果你想选择一个不错的框架,而不是重新发明 MVC 轮子,我强烈建议,LARAVEL 4