0

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
  • 1
    you send parameters with your ajax-call to the php file. so you are also able to set some "flag parameters" like ?delete=true&id=1. if delete isset = true -> call delete_account() with this way you can send many different parameters from your ajax call to your php file and choose the function which you want execute Commented Nov 1, 2013 at 10:25

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I keep getting an undefined index error on this: $action = $_GET['action'];
@user1476992 you have not used 'action' parameter in your url, change your ajax url to like:: url: 'inc/functions.php?action=functionToCall'
I'm sorry, I've updated my ajax since I pasted it here. It includes the query string. I think the problem resides in my main php file, where I'm calling the get_accounts() function and assigning it to an $accounts variable. How can I call the switch statement from within my variable assignment? i.e. $accounts = "get_accounts"; or $accounts = "action=get_accounts";
I'm sorry, I wasn't explaining myself well. I've got the AJAX and PHP piece sorted out thanks to your advice, but in a totally different area of my code, I'm trying to use an $accounts variable. It was working fine when it just said, $accounts = get_accounts(); but then when I moved the get_accounts() function into the switch statement, now it doesnt work. I'd still like to keep the $accounts variable, but I just don't know how to call the get_accounts() function from the variable declaration now that the function itself is inside the switch statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.