doulun0651 2013-11-01 10:21
浏览 17
已采纳

多个PHP函数在同一文件中的问题

I want to make an AJAX call to a php file called functions.php, where I have mutliple related functions (all making database changes to accounts, ie. add, edit, delete). I've been struggling with this, because the AJAX call seems to work fine, but the response that comes back from the server is empty (content-lenght: 0). I went back to square one and just used AJAX to send data to a php file that only contained the php code to handle that one call (ie. no functions), and it works fine. As soon as I wrap that simple statement into a function, it fails again. So something I'm doing causing the php not to send a respone when I'm wrapping it in a function.

Do you have to call a specific php function somehow from your ajax script, or somewhere in your form element? How does the php file know which function you're requesting in your ajax call if there are multiple functions? Or does it just sort it out by matching the $_POST element with the data being sent over, regardless of which function that $_POST element is in?

I suspect that my PHP code is to blame:

function delete_account(){
require(ROOT_PATH . "/inc/database.php");

$deleteAccount = $_POST['accountName'];

try {
    $results = $db->prepare("DELETE FROM account WHERE account_id_PK = ?");
    $results->bindValue(1, $deleteAccount);
    $results->execute();
} catch(Exception $e) {
    echo "ERROR: Data could not be removed from the database. " . $e;
    exit;
}
return;
}

Also, when I press the submit button and my ajax call is fired off, it immediately returns the success message in the browser, and as I'm watching the Network tab on my google dev tools window, the success message is displayed seemingly before the php file is even loaded. I've tried setting async: false.

Adding AJAX code:

function deleteAccount(){
    event.preventDefault();

    var accountName = $('.account_name').filter(":selected").attr("name");

    $.ajax({
        type: "POST",
        url: 'inc/functions.php', 
        data: {accountName:accountName}, 
        async: false,
        success: function(response){
        $('#results').html(response + " has been deleted.");
        }
    });
};
  • 写回答

1条回答 默认 最新

  • duanfenhui5511 2013-11-01 10:27
    关注

    If you have multiple functions in your PHP file, then you can do something like:: pass in extra parameter in you jQuery ajax, like:

    $.ajax({
        url: "some_file.php?action=get_accounts"
    }).done(function() {
        $( this ).addClass( "done" );
    });
    

    and in your PHP file use switch to call appropriate function depending on value of action variable in your ajax, like:

    //in PHP
    .....
    $action = $_GET['action'];
    switch ($action) {
        case "get_accounts":
            //call the function
            get_accounts();
            break;
        case "otherFunction":
            ....
            break;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)