0

Here is a very simple and silly question I'm going to ask:

I'm very new to PHP, I was wondering if there is a way to call just a function in a class of PHP by AJAX.

for instance something like this:

$.ajax({
        url:'newPHPClass.php/My_Function_Name'
        ...
});

So in php, it would be like:

<?php
class newPHPClass {
    function My_Function_Name(){

    }
}
?>

Why do I need this? So I wouldn't have like so many php files. Instead I'll have a single php class with different functions inside. It will look cleaner and less extra files.

I have used ASP.NET MVC C# before. and in ASP.NET I could just simply put my controller's name in the URL section of AJAX followed by / and then my function's name (which could be modified in my Global.asax.cs file).

Thanks.

3
  • Sure, just use a querystring instead, and capture the function name from that (sending the function name in $.ajax data: parameter would do the same), and execute that function? Commented Feb 18, 2013 at 1:07
  • Just watch what functions you allow people to execute by submitting a request in the url... you wouldn't want people submitting malicious requests Commented Feb 18, 2013 at 1:08
  • 1
    You have used frameworks in ASP so you could have passed your controller name and method name easily because those frameworks were designed with routing but if you want to do same thing in php then it's possible using php frameworks (like laravel, or other) otherwise you can't just pass class name and method name, it won't work this way. Commented Feb 18, 2013 at 1:47

2 Answers 2

1
<?php

$action = $_POST['action'];

class Foo {

    function execute($action) {
        switch($action) {
            case "method1":
                $this->doMethod1();
                break;
            case "method2":
                $this->doMethod2();
                break;
        }
    }

    function doMethod1() {
        echo "Foo::doMethod1";
    }

    function doMethod2() {
        echo "Foo::doMethod2";
    }
}

$foo = new Foo();
$foo->execute($action);

?>

And use foo.php?action=method1 as URL.

Sign up to request clarification or add additional context in comments.

2 Comments

I had exactly the same thing in my mind as a walking-around-the-issue, after reading Sheikh Heera's comment on quetion. Anyways, thanks a bunch mate :)
Why? Nobody is saying that the methods of this class have to contain all the logic. This could be a class named Router and use its methods to wrap calls to a separate class. Btw, I don't think either this is the best OOP approach, but I'm just answering OP's question ;)
1

First of all you don't call functions or methods via AJAX, you make a request to a page. If you make an AJAX call to a PHP file like:

<?php
class newPHPClass {
    function My_Function_Name(){

    }
}
?>

nothing will be returned and nothing will happen server side other than allocating the space for that class and then immediately destroy it at the end of the request.

For your question: it doesn't really matter on the client side (Javascript) how you are going to manage the class and files organization: only one file is going to be requested by an AJAX call.

I'd suggest you to have at least two different files: the declaration of functions or classes and the file that manage the response. You need a bit a separation from execution and declaration, therefore I'd avoid things like:

class A { ... }
$a = new A;
$a->myMethod();

Depending on the AJAX output I'd go with something like this instead:

# class.php
class A {...}

# ajax.php
$a = new A;
$a->myMethod();

Finally a suggestion: don't use classes when they just don't make sense. Having a bunch of functions grouped inside a class doesn't really feel OOP to me. I think you are using classes with procedural code in mind. Therefore I would go with a file that declares a bunch of functions instead.

1 Comment

Jeffrey, Thank you for your good explanation. However, I used the newPHPClass for just a name reference in my question for easier understand for others visiting this page. Also about one file being able to be called by an AJAX call, I am well aware of that, but I'm having multiple AJAX calls in one page since I have many cascading drop downs. Therefore, I asked this question so I won't have many different pages with almost exactly the same content (just few variables would be different) in my website. Which I think A. Rodas gave a very good answer for this. Thanks for your time and answer.

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.