donglang7236 2014-01-25 18:46
浏览 14
已采纳

从ajax调用访问PHP函数

I am trying to access a PHP function from an ajax call but I am running into all sorts of issues since is my first time doing something like this. I need help creating the function or fixing the one I have.

This is my ajax call (I will only post a small part of it since the complete function is irrelevant to the question)

 $.ajax({
    type        : 'post', 
    url         : 'data.php?action=checkname',
    data        : {'location':locationName},
    error       : function(data,status,error){
                console.log(data+': '+status+': '+error);
            },
    success     : function(res){
                console.log(res);
            }                   
    });

PHP function

<?php
    function checkname(){   <--- What do I pass in the function???

        $locations = $_POST['location'];

        echo $locations;

    }
?>

If I do this the "simple way" by just calling the file and do something like

$locations = $_POST['location'];

echo $locations;

I get the return that I need but it will be wrong to just have a small file for all the ajax calls that I need to create.

  • 写回答

4条回答 默认 最新

  • dongzhuoxie1244 2014-01-25 19:02
    关注

    You can setup for php doing something like this. Use a Switch and give each action a name in your ajax the name of the Case you want to activate. This way you can use the same file and call different functions as you see fit.

    <?php
                $action = $_POST["action"];
                //check if it was sent using $_GET instead of $_POST 
                if(!$action)
                $action = $_GET["action"];
    
                switch($action){
                    case 'checkname':
                     checkname();
                    break;
    
                    case 'otherfunction':
                     otherfunction();
                    break;
    
                 }//switch
    
    
         function checkname(){   
    
                    $locations = $_POST['location'];
    
                    echo $locations;
    
         }
    
         function otherfunction(){  
    
                    //do something else
    
                    //echo something;
    
         }
    ?>
    

    I didn't put the ajax because you already had the ajax call. This part of your ajax is the name you will use for the action. data.php?action=checkname or you can use the data like this.

    var action = 'checkname';
    
    $.ajax({
        type        : 'post', 
        url         : 'data.php',
        data        : {'location':locationName,'action':action},
        error       : function(data,status,error){
                    console.log(data+': '+status+': '+error);
                },
        success     : function(res){
                    console.log(res);
                }                   
        });
    

    You can use other ajax functions and just change variable action to the function you want to call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline