gpt4 book ai didi

javascript - 如何使用 jQuery ajax 调用 PHP 函数?

转载 作者:行者123 更新时间:2023-11-29 18:09:59 26 4
gpt4 key购买 nike

我有一个名为 myfunctions.php 的文件,其中有很多函数,例如

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()。我该怎么做?

最佳答案

在 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",

关于javascript - 如何使用 jQuery ajax 调用 PHP 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28291183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com