php 从jquery调用php函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5578899/
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
calling php function from jquery?
提问by mr_eclair
Possible Duplicate:
Call PHP function from jQuery?
可能的重复:
从 jQuery 调用 PHP 函数?
Jquery on button click call php function
Jquery按钮点击调用php函数
$('.refresh').click(
function(){
<?php hello(); ?>
}
)
PHP Code
PHP代码
<?php
function hello()
{
echo "bye bye"
}
?>
I Want to call a php function from jquery on button click how can i do this ?
我想在单击按钮时从 jquery 调用 php 函数,我该怎么做?
回答by Dmitry Evseev
From jQuery you can only call php script with this function. Like that:
在 jQuery 中,您只能使用此函数调用 php 脚本。像那样:
$.ajax({
url: 'hello.php',
success: function (response) {//response is value returned from php (for your example it's "bye bye"
alert(response);
}
});
hello.php
你好.php
<?php
echo "bye bye"
?>
回答by ITroubs
your JS
你的JS
$('.refresh').click(
function(){
$.ajax({
url: "ajax.php&f=hello",
type: "GET"
success: function(data){
//Do something here with the "data"
}
});
}
)
your ajax.php
你的 ajax.php
<?php
$validFunctions = array("hello","anotherF");
$functName = $_REQUEST['f'];
if(in_array($functName,$validFunctions))
{
$$functName();
}else{
echo "You don't have permission to call that function so back off!";
exit();
}
function hello()
{
echo "bye bye";
}
function anotherF()
{
echo "the other funct";
}
function noTouch()
{
echo "can't touch this!";
}
?>
this is a little example of really basic and pretty ugly RMI type invocation of php methods via ajax
这是通过 ajax 调用 php 方法的非常基本和非常丑陋的 RMI 类型的一个小例子
回答by gnur
PHP is something that is executed on the server, the client (browser) has no direct access to any of the PHP code that is being executed. This is very nice because otherwise everyone could access all files and your entire mysql database.
PHP 是在服务器上执行的东西,客户端(浏览器)无法直接访问任何正在执行的 PHP 代码。这非常好,否则每个人都可以访问所有文件和整个 mysql 数据库。
You can combine php and javascript (jquery) with AJAX or a library like xajax.
您可以将 php 和 javascript (jquery) 与 AJAX 或类似 xajax 的库结合使用。
回答by Fivell
php is server side language, js - client side... I think you should use ajax. or your php function should return valid javascript code
php 是服务器端语言,js - 客户端......我认为你应该使用 ajax。或者你的 php 函数应该返回有效的 javascript 代码