Php 使用Jquery调用方法或函数
所以我可以使用jquery调用php页面 $.ajax({type:“GET”, url:“refresh_news_image.php”, 数据:“name=“+name 成功:函数(html){ 警报(html) $('div.imageHolder').html(html) })) 但是这有点混乱,我有几个.php文件,它们只执行非常简单的任务 $images->refresh_image() 这可能吗?如果失败,我可以创建一个包含很多函数的大文件 谢谢Php 使用Jquery调用方法或函数,php,function,methods,call,Php,Function,Methods,Call,所以我可以使用jquery调用php页面 $.ajax({type:“GET”, url:“refresh_news_image.php”, 数据:“name=“+name 成功:函数(html){ 警报(html) $('div.imageHolder').html(html) })) 但是这有点混乱,我有几个.php文件,它们只执行非常简单的任务 $images->refresh_image() 这可能吗?如果失败,我可以创建一个包含很多函数的大文件 谢谢 Ross您不能直接从jQuery调
Ross您不能直接从jQuery调用php函数,因为jQuery对php一无所知。您可以创建一个文件,根据给定的请求参数调用相应的函数并返回结果。当然,这取决于意图是什么,但如果您真的只想运行refresh\u image函数,您可以这样做不需要传递任何数据,也不需要处理成功等问题,这样就不麻烦了:
$.ajax({
url: "refresh_news_image.php",
});
您还可以创建一个通用函数,例如:
function php_func(name){
$.ajax({
data: { name: name }
url: "background_functions.php",
});
}
然后是background_functions.php:
switch($_GET['name']){
case 'refresh_image':
$images->refresh_image();
break;
case 'something else':
something_else();
break;
}
在javascript中,每当您需要它时(可能在onclick上),您只需调用:
php_func('refresh_images');
小心不要使用GET参数来运行传递的任何函数,因为这显然是一个巨大的安全风险。:-)
switch($_GET['name']){
case 'refresh_image':
$images->refresh_image();
break;
case 'something else':
something_else();
break;
}