如何使用jQuery ajax调用PHP函数? [英] How to call PHP function using jQuery ajax?
本文介绍了如何使用jQuery ajax调用PHP函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为myfunctions.php的文件,其中有很多功能,例如
I have a file called myfunctions.php where I have a lot of functions, like
function sendForm(){
//save form
}
function fn2(){
//do something
}
// Other functions ...
和jquery代码,
$.ajax({
url: "myfunctions.php",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: {key1: "value1", key2: "value2", key3: "value3"},
complete: function(){
//completado
alert("complete");
}
});
我需要在此文件中调用特定的函数;例如sendForm()
.我该怎么办?
I need call specific function in this file; for example sendForm()
. How can I do that?
推荐答案
在PHP中
<?php
// create a list of approved function calls
$approved_functions = array('sendForm','fn2');
// check the $_GET['function'] and see if it matches an approved function
if(in_array($_GET['function'], $approved_functions))
{
// call the approved function
$_GET['function']();
}
function sendForm(){
//save form
}
function fn2(){
//do something
}
在AJAX中
// specify which function to call
url: "myfunctions.php?function=sendForm",
这篇关于如何使用jQuery ajax调用PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文