如何从 javascript onclick() 事件调用 php 函数?

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

How do I call a php function from javascript onclick() event?

phpjavascriptdrupal-6

提问by Brian T Hannan

For example, I have a php function:

例如,我有一个 php 函数:

function DeleteItem($item_id)
{
     db_query("DELETE FROM {items} WHERE id=$item_id");
}

Then, I have something like the following:

然后,我有如下内容:

<html>
<head>
<script type="text/javascript">
function DeleteItem($item_id)
{
     alert("You have deleted item #"+$item_id);
}
</script>
</head>

<body>
<form>
Item 1
<input type="button" value="Delete" onclick="DeleteItem(1)" />
</form>
</body>
</html>

I want to be able to call the PHP function DeleteItem() from the javascript function DeleteItem() so that I can use Drupal's db_query() function, so I don't have to try to establish a connection to the database from javascript.

我希望能够从 javascript 函数 DeleteItem() 调用 PHP 函数 DeleteItem(),以便我可以使用 Drupal 的 db_query() 函数,因此我不必尝试从 javascript 建立到数据库的连接。

Does anyone have any suggestions on how this might be done? P.S. I understand that PHP processes on the server-side and javascript processes on the client-side, so please no responses saying that. There has got to be some kind of trick one can do in order to have this work out. Or maybe there is a better way of doing what I am trying to accomplish.

有没有人对如何做到这一点有任何建议?PS 我知道 PHP 在服务器端处理,在客户端处理 javascript,所以请不要回应说。必须有某种技巧才能完成这项工作。或者也许有更好的方法来做我想要完成的事情。

回答by bmb

Since you are aware that PHP processes on the server-side and javascript processes on the client-side, you must also realize you can't call a PHP "function" from javascript. Your client side code can redirect to a PHP page, or invoke a PHP program using AJAX. That page or program must be on the server and it should do a lot more than just the one line you have in your function. It should also check for authentication, authorization, etc. You don't want just any client side script anywhere to call your PHP.

由于您知道 PHP 在服务器端处理,而 JavaScript 在客户端处理,您还必须意识到您不能从 javascript 调用 PHP“函数”。您的客户端代码可以重定向到 PHP 页面,或使用 AJAX 调用 PHP 程序。该页面或程序必须在服务器上,并且它应该做的不仅仅是您函数中的一行。它还应该检查身份验证、授权等。您不希望任何地方的任何客户端脚本都可以调用您的 PHP。

回答by Marek Karbarz

You will want to use ajaxfor that. Also, database connection from within javascript is something you should not even consider as an option - terribly insecure.

为此,您将需要使用ajax。此外,来自 javascript 的数据库连接是您甚至不应该考虑的选项 - 非常不安全。

A very simple example:

一个非常简单的例子:

//in javascript
function DeleteItem($item_id) {
    $.post("delete.php", { id: $item_id}, function(data) {
        alert("You have deleted item #"+$item_id);
    });
}

//in php file
db_query("DELETE FROM {items} WHERE id=" . $_REQUEST["id"]);

回答by Christoph

You need to write a PHP script which will execute the function. To call it, either:

您需要编写一个 PHP 脚本来执行该函数。要调用它,要么:

  • use XMLHttpRequest(aka Ajax) to send the request
  • change the page's location.hrefand return HTTP status code 204 No Contentfrom PHP
  • 使用XMLHttpRequest(又名 Ajax)发送请求
  • 更改页面location.href204 No Content从 PHP返回 HTTP 状态代码

回答by simonrjones

you can't actually call PHP functions within JavaScript per se. As @Christoph writes you need to call a PHP script via a normal HTTP request from within JavaScript using the magic that is known as AJAX (silly acronym, basically means JS can load external HTTP requests on the fly).

您实际上无法在 JavaScript 中调用 PHP 函数本身。正如@Christoph 所写,您需要使用称为 AJAX 的魔法通过 JavaScript 中的普通 HTTP 请求调用 PHP 脚本(愚蠢的首字母缩写词,基本上意味着 JS 可以即时加载外部 HTTP 请求)。

Take a look at jQuery's AJAX functionality on how to reliably make a HTTP request via JS, see http://docs.jquery.com/Ajax

查看 jQuery 的 AJAX 功能,了解如何通过 JS 可靠地发出 HTTP 请求,请参阅http://docs.jquery.com/Ajax

All the normal security rules apply, i.e. make sure you filter incoming data and ensure it's what you're expecting (the $item_id in your example). Bear in mind there's nothing to stop someone manually accessing the URL requested by your JS.

所有正常的安全规则都适用,即确保您过滤传入的数据并确保它是您所期望的(在您的示例中为 $item_id)。请记住,没有什么可以阻止某人手动访问您的 JS 请求的 URL。

回答by Joel L

First, use jQuery.

首先,使用jQuery。

Then, your code will have to be something like:

然后,您的代码必须类似于:

<input ... id="item_id_1" />

<script>
$(document).ready(function() {
   $('input').click(function(){
      var item_id = $(this).attr('id');
      item_id = item_id.split('_id_');
      item_id = item_id[1];
      $.get('/your_delete_url/' + item_id);
   });
});
</script>