如何使用函数在 php 中实现 ajax



我想使用 json、ajax 和 php 构建一个 Web 应用程序,以便更清楚,我需要一个可以从 ajax 方法调用的具有不同函数的 php 页面

例如:phppage.php

addmydetails() 
{
logic goes here
}

阿贾克斯页面

/* Get from elements values */
var values = $(this).serialize();
$.ajax({
url: "phppage.php/addmydetails",
type: "post",
data: values ,
success: function (response) {
// you will get response from your php page (what you echo or print)                 
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}

});

所以我的实际问题是,在 ajax 帖子中调用此类函数的任何选项URL: "phppage.php/addmydetails">

无需重写 URL。只需获取 PATH 并使用call_user_func。像这样:

在你的phppage.php:

$method = trim($_SERVER["PATH_INFO"], "/");
if(function_exists($method)){
call_user_func($method);
exit();
}
function Your_function_name(){
print "this is my function";
}

然后你可以使用这个网址访问它 --> phppage.php/Your_function_name

最新更新