Jquery Ajax and php classes

2019-04-02 01:39发布

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.

9条回答
再贱就再见
2楼-- · 2019-04-02 02:14

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.

查看更多
Animai°情兽
3楼-- · 2019-04-02 02:15

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();
    }
?>
查看更多
叛逆
4楼-- · 2019-04-02 02:17

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.

查看更多
够拽才男人
5楼-- · 2019-04-02 02:18

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.

查看更多
不美不萌又怎样
6楼-- · 2019-04-02 02:21

Yes! All the 'no' answers aren't quite clear enough.You could do it like this:

$.post("processAction.php", { name: value }, myFunction);

Then processAction.php would check for your class and run it's functions.

require("myClass.class.php");
session_start();

if (!isset($_SESSION['myClass'])){
   $_SESSION['myClass'] = new myClass();
}
$class = $_SESSION['myClass']; 

$data = $_POST['name'];
//run function
$class->functionName($data);

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.

查看更多
ら.Afraid
7楼-- · 2019-04-02 02:22

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

查看更多
登录 后发表回答