How to call a function in PHP class via AJAX

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
EnDee321
Forum Newbie
Posts: 6
Joined: Tue Nov 01, 2011 9:19 am

How to call a function in PHP class via AJAX

Post by EnDee321 »

Hi all,
Can we call a function in a PHP class and get the result using AJAX? If can how?

Regards.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to call a function in PHP class via AJAX

Post by califdon »

Remember that AJAX is simply going to call a PHP script on the server, which certainly can instantiate a class and call a method of that class, then return the data, which will be handled by the AJAX function.
EnDee321
Forum Newbie
Posts: 6
Joined: Tue Nov 01, 2011 9:19 am

Re: How to call a function in PHP class via AJAX

Post by EnDee321 »

califdon wrote:Remember that AJAX is simply going to call a PHP script on the server, which certainly can instantiate a class and call a method of that class, then return the data, which will be handled by the AJAX function.

Thanks for the reply califdon. Yes what you have mentioned is the traditional way of doing the thing.
I'm working on a research for Object-oriented designing and development on AJAX enabled RIAs. I want to see if/how others develop a direct AJAX calling to a function in a PHP class, without additional (Non Object-eriented) PHP pages. Any solutions from others around?

Please share your experience.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to call a function in PHP class via AJAX

Post by califdon »

AJAX is defined as the use of the native Javascript object XMLHttpRequest. Using that object, you can send an asynchronous request for data to the web server. All of this happens within the scope of the user's browser on the user's computer. Once the request is received by the web server, it is sent to the requested script, which executes on the server. As far as I know, that defines the scope of what AJAX can do. I don't understand what you mean by a "direct" call "to a function in a PHP class", nor what you call "Non Object-oriented PHP pages".
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to call a function in PHP class via AJAX

Post by pickle »

You MUST call a URL with AJAX. You therefore MUST have a page that is run when that URL is accessed. You can't directly call a PHP function via AJAX because there is no way for Javascript in a browser to run PHP directly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
EnDee321
Forum Newbie
Posts: 6
Joined: Tue Nov 01, 2011 9:19 am

Re: How to call a function in PHP class via AJAX

Post by EnDee321 »

Thanks a lot for participating this thread guys.
Yes you both are correct. Let me explain some background before jump into more clarification on my problem domain.

PHP started as a structured language. OOP was introduced in version 3 and became popular with version 5 (Guess Im correct?) When an AJAX call is initiated, the request goes to the page stated in the URL and executes a structured coding. Yes we can create any objects from any class and execute any methods in those classes and get the result by executing that structure code. But,,, my problem is..... if the AJAX URL is pointed to a PHP page with a class definition; is there a way to call a specific method in that class? So that the advantage is; the same PHP page can be used by many AJAX calls, to execute different methods at different times. If you are familiar with ASP.NET code behind structure (DAL,BLL) you may know what Im saying (When you click a button, the specific method in the server is executed)
This is basically not about doing the work somehow, its about the architecture and the structure.
Please continue discussions on this and share your knowledge with me.
Thanks again.

Regards
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to call a function in PHP class via AJAX

Post by pickle »

No, there is no way. You need other code to call the class methods. You COULD set up the page to handle requests in such a way that you get that flexibility. For example, you could request the page like: /page.php?function=multiply&args[]=8&args[]=2. And have that result in your code calling:

Code: Select all

$MyObject = new MyClass();
$result = $MyObject->multiply($_GET['args']);
echo $result;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
EnDee321
Forum Newbie
Posts: 6
Joined: Tue Nov 01, 2011 9:19 am

Re: How to call a function in PHP class via AJAX

Post by EnDee321 »

Dear pickle,
By starting "No", you have proposed a way to satisfy the requirement. :D This is closer to what Im doing right now. Its ok to have a structured coding segment in the file, but the script should be able to call any method in the class and and return the result via the AJAX response, and the mechanism should be more general. I never thought of the args[] and I have to try it out and see whether I can handle all the criteria easier than my algorithm. Thanks for posting pickle. Please keep improving, I'll share my knowledge too. (Im waiting till others to come up with more solutions) :)

Any other tips/tricks/ways of handling the problem guys?
Post Reply