我如何通过JavaScript调用PHP函数? [英] How can I call PHP functions by JavaScript?

查看:106
本文介绍了我如何通过JavaScript调用PHP函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个外部php文件调用一个php函数到javascript中。我的代码不同而且很大,所以我在这里写了一个示例代码。



这是我的php代码:

 <?php 
函数add($ a,$ b){
$ c = $ a + $ b;
返回$ c;
}
函数mult($ a,$ b){
$ c = $ a * $ b;
返回$ c;
}

函数除法($ a,$ b){
$ c = $ a / $ b;
返回$ c;
}
?>

这是我的javascript代码:

 <脚本> 
var phpadd = add(1,2); //调用php添加函数
var phpmult = mult(1,2); //调用php函数
var phpdivide = divide(1,2); //调用php分隔函数
< / script>

所以这就是我想要做的。



我的原始php文件不包含这些数学函数,但是这个想法是相同的。



如果它没有适当的解决方案,那么请建议我它可以调用来自外部php的值。

解决方案

是的,您可以使用数据在服务器上执行ajax请求请求参数,像这样(非常简单):

请注意,以下代码使用 jQuery

  jQuery.ajax({
类型:POST,
url:'your_functions_address.php ',
dataType:'json',
data:{functionname:'add',arguments:[1,2]},

成功:function(obj,textstatus) {
if(!('error'in obj)){
yourVariable = obj.result;
}
else {
的console.log(obj.error);
}
}
});

和your_functions_address.php是这样的:

 <?php 
header('Content-Type:application / json');

$ aResult = array();

if(!isset($ _ POST ['functionname'])){$ aResult ['error'] ='没有函数名!'; }
$ b $ if(!isset($ _ POST ['arguments'])){$ aResult ['error'] ='没有函数参数!'; }
$ b $ if(!isset($ aResult ['error'])){

switch($ _ POST ['functionname']){
case'add ':
if(!is_array($ _ POST ['arguments'])||(count($ _ POST ['arguments'])< 2)){
$ aResult ['error'] = '参数错误!';
}
else {
$ aResult ['result'] = add(floatval($ _ POST ['arguments'] [0]),floatval($ _ POST ['arguments'] [1 ]));
}
break;

默认值:
$ aResult ['error'] ='未找到函数'$ _ POST ['functionname']。'!';
休息;
}

}

echo json_encode($ aResult);

?>


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

This is my php code:

<?php
function add($a,$b){
  $c=$a+$b;
  return $c;
}
function mult($a,$b){
  $c=$a*$b;
  return $c;
}

function divide($a,$b){
  $c=$a/$b;
  return $c;
}
?>

This is my javascript code:

<script>
  var phpadd= add(1,2); //call the php add function
  var phpmult= mult(1,2); //call the php mult function
  var phpdivide= divide(1,2); //call the php divide function
</script>

So this is what I want to do.

My original php file doesn't include these mathematical functions but the idea is same.

If somehow it doesn't have a proper solution, then please suggest me the alternative, but it should call values from external php.

解决方案

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:

    <?php
    header('Content-Type: application/json');

    $aResult = array();

    if( !isset($_POST['functionname']) ) { $aResult['error'] = 'No function name!'; }

    if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }

    if( !isset($aResult['error']) ) {

        switch($_POST['functionname']) {
            case 'add':
               if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 2) ) {
                   $aResult['error'] = 'Error in arguments!';
               }
               else {
                   $aResult['result'] = add(floatval($_POST['arguments'][0]), floatval($_POST['arguments'][1]));
               }
               break;

            default:
               $aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
               break;
        }

    }

    echo json_encode($aResult);

?>

这篇关于我如何通过JavaScript调用PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆