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.
相关问题
- Views base64 encoded blob in HTML with PHP
- how to define constructor for Python's new Nam
- Laravel Option Select - Default Issue
- Carriage Return (ASCII chr 13) is missing from tex
- PHP Recursively File Folder Scan Sorted by Modific
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.
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.
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.
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...
Then add the jQuery somewhere in a script tag:
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...
And then all you gotta do is get some data to a php script that looks something like this...
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.
Yes! All the 'no' answers aren't quite clear enough.You could do it like this:
Then processAction.php would check for your class and run it's functions.
This is just a basic example and probably has errors, but hopefully you get the general idea. It's not direct, but it does allow you to keep your classes.
No, you can't directly asscess object's method, but you can request file, which executes that method.