javascript 通过 HTML 链接调用 PHP 函数(无格式)

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

Calling a PHP function through an HTML Link (no form)

phpjavascripthtmlinput

提问by pdt2383

I have a PHP Function that I would like to integrate into my (existing) web page. Further, I would like it to execute when the user clicks a link on the page. The function needs to accept the text of the link as an input argument.

我有一个 PHP 函数,我想将它集成到我的(现有)网页中。此外,我希望它在用户单击页面上的链接时执行。该函数需要接受链接的文本作为输入参数。

Everything I've researched for sending data to a PHP script seems to involve using forms to obtain user input. The page needs to accept no user input, just send the link-text to the function and execute that function.

我为将数据发送到 PHP 脚本而研究的所有内容似乎都涉及使用表单来获取用户输入。该页面不需要接受任何用户输入,只需将链接文本发送到该函数并执行该函数。

So I guess the question is two-part. First, how to execute a PHP script on link click. And second, how to pass page information to this function without the use of forms. I am open to the use of other technologies such as AJAX or JavaScript if necessary.

所以我想这个问题分为两部分。首先,如何在链接点击时执行 PHP 脚本。其次,如何在不使用表单的情况下将页面信息传递给这个函数。如有必要,我愿意使用其他技术,例如 AJAX 或 JavaScript。

EDIT:: Specifically what I am trying to do. I have an HTML output representing documentation of some source code. On this output is a series of links (referring to code constructs in the source code) that, upon being clicked, will call some python function installed on the web server (which leads me to think it needs called via PHP). The python function, however, needs the name present on the link as an input argument.

编辑:: 特别是我想要做的。我有一个 HTML 输出表示一些源代码的文档。在此输出中是一系列链接(指源代码中的代码结构),单击这些链接后,将调用安装在 Web 服务器上的一些 python 函数(这让我认为它需要通过 PHP 调用)。但是,python 函数需要链接上的名称作为输入参数。

Is there some sort of interaction I could achieve by having JavaScript gather the input and call the PHP function?

我可以通过让 JavaScript 收集输入并调用 PHP 函数来实现某种交互吗?

Sorry for the vagueness, I am INCREDIBLY new to web development. If anything is unclear let me know.

抱歉,含糊不清,我对 Web 开发非常陌生。如果有什么不清楚的,请告诉我。

采纳答案by Seth

You will need to use AJAX to accomplish this without leaving the page. Here is an example using jQuery and AJAX (this assumes you have already included the jQuery library):

您将需要使用 AJAX 来完成此操作而无需离开页面。这是一个使用 jQuery 和 AJAX 的示例(假设您已经包含了 jQuery 库):

First File:

第一个文件:

<script language="javascript">

$(function(){
    $('#mylink').click(function(){
        $.get('/ajax/someurl', {linkText: $(this).text()}, function(resp){
           // handle response here
        }, 'json');
    });

});

</script>

<a href="#" id="mylink">This text will be passed along</a>

PHP File:

PHP文件:

$text = $_REQUEST['linkText'];
// do something with $text here

回答by Amal Murali

You'll need to have a JS function which is triggered by an onclick event which then sends an AJAX request and returns false (so it won't be redirected to a new page in the browser). You can do the following in jQuery:

您需要有一个由 onclick 事件触发的 JS 函数,该函数然后发送一个 AJAX 请求并返回 false(因此它不会被重定向到浏览器中的新页面)。您可以在 jQuery 中执行以下操作:

jQuery:

jQuery:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
    $.get("myfile.php");
    return false;
}
</script>

And in your page body:

在您的页面正文中:

<a href="#" onclick="doSomething();">Click Me!</a>

In myfile.php:

myfile.php 中

You can add whatever function you want to execute when the visitor clicks the link. Example:

您可以添加在访问者单击链接时要执行的任何功能。例子:

<?php
echo "Hey, this is some text!";
?>

That's a basic example. I hope this helps.

这是一个基本的例子。我希望这有帮助。

回答by Daniel W.

If you are familiar with jQuery, you could do the following, if you don't want the site to redirect but execute your function:

如果您熟悉 jQuery,如果您不希望站点重定向但执行您的函数,则可以执行以下操作:

in your html head:

在你的 html 头中:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

the link:

链接:

<a href="#" onclick="$.get('ajax.php');">Execute function</a>

in ajax.php you put in your function to be executed.

在 ajax.php 中,您放入要执行的函数。

回答by drull

Maybe something like this:

也许是这样的:

<a href="#" onclick="return sendText(this);"></a>
....
<script>
function sendText(e)
{
 $.ajax({
         url: '/your/url/',
         data: {text: $(e).html()},
         type: 'POST' 
        });
}
</script>

回答by masnun

You can use query strings for this. For example if you link to this page:

您可以为此使用查询字符串。例如,如果您链​​接到此页面:

example.php?text=hello

(Instead of putting a direct link, you can also send a ajax GET request to that URL)

(您也可以向该 URL 发送 ajax GET 请求,而不是放置直接链接)

Inside example.php, you can get the value 'hello' like this:

在 example.php 中,您可以像这样获得值 'hello':

<?php
$text = $_GET['hello'];

Then call your function:

然后调用你的函数:

myfunction($text); 

Please make sure you sanitize and validate the value before passing it to the function. Depending on what you're doing inside that function, the outcome could be fatal!

请确保在将值传递给函数之前对其进行清理和验证。根据您在该函数中执行的操作,结果可能是致命的!

This links might help:

此链接可能会有所帮助:

回答by landons

Here's an overly simplistic example of what you're trying to do..

这是您尝试执行的操作的一个过于简单的示例。

Your link:

您的链接:

<a href="somefile.php?action=Some+Action">Some Action</a>

Your PHP file:

您的 PHP 文件:

<?php

if (isset($_GET['action']))
{
    // make sure to validate your input here!
    some_function($_GET['action']);
}

回答by vogomatix

PHP is a server side language i.e. it doesn't run in the web browser.

PHP 是一种服务器端语言,即它不能在 Web 浏览器中运行。

If you want a function in the browser to operate on clicking a link you are probably talking about doing some Javascript.

如果您希望浏览器中的功能在单击链接时进行操作,您可能正在谈论做一些 Javascript。

You can use the Javascript to find the text value contained in the link node and send that to the server, then have your PHP script process it.

您可以使用 Javascript 查找链接节点中包含的文本值并将其发送到服务器,然后让您的 PHP 脚本对其进行处理。