Call PHP Function using jQuery AJAX

后端 未结 3 1517
野趣味
野趣味 2020-11-30 12:19

I\'ve read all the topics about my question but cannot solve my problem. I want to get php function result using jQuery AJAX.

function.php

function g         


        
3条回答
  •  执笔经年
    2020-11-30 12:36

    What I do is in JavaScript I pass PHP function name which I want to call. Like..

    function MyFunction() {
    jQuery.ajax({
        type: "GET",
        url: "function.php",
        data: "call=generateCode",
        success: function(response){
            //do something
        });
    }
    

    Here in data field "call" variable have function name to call from "function.php" file.

    in "function.php" I place below code to call the function

    if($_SERVER['REQUEST_METHOD']=="GET") {
    $function = $_GET['call'];
    if(function_exists($function)) {        
        call_user_func($function);
    } else {
        echo 'Function Not Exists!!';
    }
    }
    

提交回复
热议问题