tags:

views:

1006

answers:

7

I'm trying to learn how to use oop in php. I'm also fairly new to jquery. Is it possible to make an Ajax request to a php class method? I've only ever sent Ajax requests to a file specifically for that purpose and that returns the data I need. Sorry if I haven't explained myself very well.

+1  A: 

Yes, of course it is, you would simply pass the value captured by one of your $_GET, $_POST or $_REQUEST superglobals to your class method.

If you're talking about 'directly invoking' a PHP class method from jQuery, then no, you can't, no in the pure sense.

karim79
A: 

No, you can't directly asscess object's method, but you can request file, which executes that method.

usoban
Could you explain that to me? If I use the jquery $.post() function, what would be the syntax?
Actually you request a file, let's say example.php and somehow indicate what method you want to get (using post data). In example.php you read that indicator and decide what object method you will call. After that, just output the result.
usoban
A: 

No, but you can map the call to the method. This is more or less what MVC helps you to do. So the call to www.mysite.com/User/Login can be easily mapped to the function Login of the class User.

You may want to have a look at Codeigniter, for example, that is a popular MVC solution.

pistacchio
+1  A: 

Although I would recommend at least understanding how to achieve this manually, you can always use Xajax which allows you to create a php class that is indirectly accessible from your page's javascript.

Karim
+2  A: 

Short answer: No.

Long answer:

Ajax is just a term for making an HTTP request from the browser, using JavaScript, without leaving the page.

The only thing you can "call" is a URL.

You can write your PHP to do something based on what the URL is though.

<?php
    if ($_POST['action'] == "delete") {
        delete();
    }
?>
David Dorward
A: 

If you want to create a way to directly pass PHP commands and directions via ajax, the only way I could see it is by using a PHP eval on code that is sent to it via post or get in jQuery. For example, in your markup, do the following...

<div id="myDiv">
Loading...
</div>

Then add the jQuery somewhere in a script tag:

myFunction(formdata) {
  data = "somekey: "+formdata;
  $("#myDiv").load("myScript.php",data);
}

Obviously to post it to that script you'll need something like a textarea that calls that javascript function when you click a link or some such...

<textarea id="myText"></textarea><br />
<a href="#" onclick="myFunction($('myText').attr('value'))" >Go!</a>

And then all you gotta do is get some data to a php script that looks something like this...

     <?php 
      if ($_POST["somekey"]) {
         eval($_POST["somekey"]);
      }
     ?>

In the end, you'll have to make sure everything is formatted properly to be put into an eval, so watch your quotes, and make sure everything is valid that's going in. Also, for the love of crap, protect this script as someone could do some serious harm if they got ahold of it.

NateDSaint
If you wanted to execute something from a class that was already instantiated in that script, you would have access to it. Also, I'd have to test this, but I think you can instantiate a class from within the eval, but it probably wouldn't work as intended.
NateDSaint
A: 

Basic answer no, but it can be done easily.

An ajax call itself cannot call class methods. It has not way of initiating the class and then calling the method. It can only cause the PHP file to run on the server via a POST/GET call on X url.

What you can do is use another file to act as a go-between from the ajax to the method. In other words, instead of calling the php file (update.php for example) directly as simple examples show, you can call a go-between file (call it whatever you like, ajax server, ajax router, etc) that uses GET/POST parameters you send to figuere out what method you want to use and what values to pass to it. You could then run the method from that file (initiating the class and calling the method with parameters), and return the results of that method from that file to the ajax calling script.

Robert DeBoer