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.
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.
No, you can't directly asscess object's method, but you can request file, which executes that method.
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.
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();
}
?>
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.
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.