Javascript 在 onclick() 事件中调用 .php 文件

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

Calling a .php file in the onclick() event

phpjavascript

提问by RKh

I want to call a .php file from the OnClick event.

我想从 OnClick 事件调用一个 .php 文件。

<input id="Edit" name="Edit" value="Edit Record" type="button" onclick="---call to php file----;" />

The code is show above. I want to call a .php file through Javascript and pass a cookie value in the query string.

代码如上所示。我想通过 Javascript 调用一个 .php 文件并在查询字符串中传递一个 cookie 值。

回答by Pekka

If you do not want to point the browser to the .php page, you are looking for AJAX.

如果您不想将浏览器指向 .php 页面,那么您正在寻找 AJAX。

AJAX with JQuery

使用 JQuery 进行 AJAX

AJAX with Prototype

带有原型的 AJAX

AJAX with Matt Kruse's Ajax toolbox

使用 Matt Kruse 的 Ajax 工具箱的 AJAX

if you want to take the browser to the .php page, it's as easy as this:

如果你想把浏览器带到 .php 页面,就像这样简单:

location.href = 'mypage.php';

回答by Thomas Bonini

I suggest using jqueryand then all you have to do is $.get("url").. It would be quite complex to do it in pure javascript.

我建议使用jquery然后你所要做的就是 $.get("url").. 在纯 javascript 中做它会非常复杂。

This is assuming you want to open the php through AJAX (ie, without actually showing the .php page to the user as it normally happens when you click a link)

这是假设您想通过 AJAX 打开 php(即,没有实际向用户显示 .php 页面,就像单击链接时通常发生的那样)

回答by Zeta Two

"...and pass a cookie value in the query string."

“...并在查询字符串中传递一个 cookie 值。”

This is not necessary since php can read the values of cookies directly through the $_COOKIES superglobal anyway. Unless you are doing some cross-domain stuff here that is.

这不是必需的,因为无论如何 php 都可以通过 $_COOKIES 超全局变量直接读取 cookie 的值。除非你在这里做一些跨域的事情。

回答by Dan Beam

you don't haveto use AJAX, you could use any external tag, like iframe, link, script, image, and homebrew your own XSS solution (which, to be honest is what it sounds like you're doing anyways...)

你不必须使用AJAX,你可以使用任何外部标记,像IFRAME,链接,文字,图像和自制自己的XSS解决方案(其中,说实话是什么,它听起来就像你做反正... )

function xssFunc( )
{
    ( new Image ).src = 'http://my.url/page.php?cookie' + encodeURIComponent( document.cookie );
}

document.getElementById( 'whatevs' ).onClick = xssFunc;

however, if you want to use the contents afterward, I'd use AJAX or an iframe (if you need to do it across domains).

但是,如果您想在之后使用这些内容,我会使用 AJAX 或 iframe(如果您需要跨域使用)。

回答by Daniel Campos

function call_php_program(code) {
    $.get("/php/php_program.php?code=" + code);
    return false;
}

onclick="call_php_program('<? echo $code; ?>')"