How can I call PHP functions by JavaScript?

后端 未结 11 1365
情话喂你
情话喂你 2020-11-22 12:08

I am trying to call a PHP function from an external PHP file into a JavaScript script. My code is different and large, so I am writing a sample code here.

This is m

11条回答
  •  时光取名叫无心
    2020-11-22 12:37

    Yes, you can do ajax request to server with your data in request parameters, like this (very simple):

    Note that the following code uses jQuery

    jQuery.ajax({
        type: "POST",
        url: 'your_functions_address.php',
        dataType: 'json',
        data: {functionname: 'add', arguments: [1, 2]},
    
        success: function (obj, textstatus) {
                      if( !('error' in obj) ) {
                          yourVariable = obj.result;
                      }
                      else {
                          console.log(obj.error);
                      }
                }
    });
    

    and your_functions_address.php like this:

        
    

提交回复
热议问题