从 javascript 执行 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16548007/
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
Execute php from javascript
提问by Matt
I'm having some trouble getting some php code working in my app. The setup is rather easy: 1 button, 1 function and 1 php file.
我在我的应用程序中运行一些 php 代码时遇到了一些麻烦。设置相当简单:1 个按钮、1 个功能和 1 个 php 文件。
script.js
脚本.js
$(document).ready(function ()
{
$("#btnTestConnectie").click(testConnectie);
});
function testConnectie()
{
$.get("script/SQL/testConnection.php");
}
testConnection.php
测试连接.php
<?php
echo "It works!";
php?>
According to this post, it should work (How do I run PHP code when a user clicks on a link?)
根据这篇文章,它应该可以工作(当用户单击链接时如何运行 PHP 代码?)
Some sources claim that it is impossible to execute php via javascript, so I don't know what to believe.
一些消息来源声称不可能通过 javascript 执行 php,所以我不知道该相信什么。
If I'm wrong, can somebody point me to a method that does work (to connect from a javascript/jQuery script to a mySQL database)?
如果我错了,有人能给我指出一个有效的方法(从 javascript/jQuery 脚本连接到 mySQL 数据库)?
Thanks!
谢谢!
回答by Damian Krawczyk
$.get('script/SQL/testConnection.php', function(data) {
alert(data)
});
You need to process Ajax result
您需要处理 Ajax 结果
回答by Tomasz Kap?oński
Javascript is run by the browser (client) and php is run on the remote server so you cannot just run php code from js. However, you can call server to run it for you and give the result back without reloading of the page. Such approach is called AJAX - read about it for a while.
Javascript 由浏览器(客户端)运行,而 php 在远程服务器上运行,因此您不能只从 js 运行 php 代码。但是,您可以调用服务器为您运行它并在不重新加载页面的情况下返回结果。这种方法称为 AJAX - 阅读它一段时间。
I see you are using jQuery - it has pretty nice API for such calls. It is documented: here
我看到您正在使用 jQuery - 它为此类调用提供了非常好的 API。它被记录在案:here
In your case the js should be rather like:
在你的情况下,js 应该是这样的:
$(document).ready(function ()
{
$("#btnTestConnectie").click($.ajax({
url: '/testConnection.php',
success: function(data) {
//do something
}
}));
});
[EDIT] Let's say you have simple script on the server that serves data from database based on id given in GET (like www.example.com/userInfo.php?id=1). In the easiest approach server will run userInfo.php script and pass superglobal array $_GET with key id ($_GET['id']=1 to be exact). In a normal call you would prepare some query, render some html and echo it so that the browser could display a new page.
[编辑] 假设您在服务器上有一个简单的脚本,该脚本根据 GET 中给出的 ID(如 www.example.com/userInfo.php?id=1)提供来自数据库的数据。在最简单的方法中,服务器将运行 userInfo.php 脚本并传递带有密钥 ID 的超全局数组 $_GET(准确地说是 $_GET['id']=1)。在正常调用中,您会准备一些查询,呈现一些 html 并回显它,以便浏览器可以显示一个新页面。
In AJAX call it's pretty much the same: server gets some call, runs a script and return it's result. All the difference is that the browser does not reload page but pass this response to the javascript function and let you do whatever you want with it. Usually you'll probably send only a data encoded (I prefer JSON) and render some proper html on the client side.
在 AJAX 调用中,它几乎是一样的:服务器得到一些调用,运行一个脚本并返回它的结果。不同之处在于浏览器不会重新加载页面,而是将此响应传递给 javascript 函数,并让您随心所欲地使用它。通常你可能只会发送一个编码的数据(我更喜欢 JSON)并在客户端呈现一些适当的 html。
回答by km6zla
You need to do something with the response that your php script is echoing out.
您需要对您的 php 脚本回显的响应做一些事情。
$.get("script/SQL/testConnection.php", function(data){
alert(data);
});
If you are using chrome of firefox you can bring up the console, enable xhr request logging and view the raw headers and responses.
如果您使用 chrome of firefox,您可以打开控制台,启用 xhr 请求日志记录并查看原始标头和响应。
回答by Jay Blanchard
You should place all of your functions in the document ready handler:
您应该将所有函数放在文档就绪处理程序中:
$(document).ready(function(){
function testConnectie() {
$.get("script/SQL/testConnection.php");
}
$("#btnTestConnectie").click(function(e) {
e.preventDefault();
testConnectie();
});
});
You will have to have your browser's console open to see the result as a response from the server. Please make sure that you change the closing PHP bracket to ?> in testConnection.php.
您必须打开浏览器的控制台才能看到作为服务器响应的结果。请确保将 testConnection.php 中的 PHP 结束括号更改为 ?>。
One other note, if you're testing AJAX functions you must test them on a webserver. Otherwise you may not get any result or the results may not be what you expect.
另请注意,如果您要测试 AJAX 函数,则必须在网络服务器上测试它们。否则您可能得不到任何结果,或者结果可能不是您所期望的。
回答by benone
You may have a look on the load() of jQuery http://api.jquery.com/load/
你可以看看 jQuery http://api.jquery.com/load/的 load()