0

I have a PHP file which consists of a number of functions, and when I tried to call one of them via ajax, it executes all the function on that page.

How can I execute only one function at a time via ajax? I am using simple PHP without class-object pattern.

0

2 Answers 2

1

Maybe you can pass a parameter in the URL telling the PHP which function you want to execute e.g.

$.ajax({
    url: "http://www.url.com/file.php?functiontocall=functiona",
    success: function(){
        // Do something
    }
});

and on the PHP side

if ($_GET["functiontocall"] == "functiona") {
    // Call functiona and output response
}
Sign up to request clarification or add additional context in comments.

Comments

0

you need 2 PHP for this. One with all the functions fileA.php, and the other which you will call through ajax fileB.php. fileB.php should include fileA.php and you can call whichever function you want.

1 Comment

An other alternative would be to pass some identifier through ajax to fileA.php, and inside fileA.php, have switch-case or if-else statements to call appropriate functions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.