Jquery Ajax and php classes

2019-04-02 01:35发布

问题:

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.

回答1:

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();
    }
?>


回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

*******************JS File test-----**************************
function actionTest(){
	$.ajax({
		type:"POST",
		url:"test2.php",
		data:{"ac":"test","pp":"YHOOOOOOOO"}
	}).done(function(data){
		$("#show").html(data);
	});
}
*******************-----PHP file test2.php-----**************************
<?php
$ac = $_POST['ac'];
$pp = $_POST['pp'];
if($ac =="test"){
	$obj = new Tt();
	$obj->test($pp);
}

class Tt
{
	
	function __construct()
	{
		
	}
	function test($aa){		
		echo "This is test function!".$aa;
	}
}
?>
*******************----HTML file------**************************
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>TESTTTTTTT</title>
	<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    <script src="js/jquery-2.1.4.js" type="text/javascript"></script>
    <script src="js/test.js" type="text/javascript"></script>     
    <link rel="shortcut icon" href="img/icons/icon.png"/>
</head>
<body onload="actionTest()">
<div id="show"></div>
</body>
</html>



回答7:

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



回答8:

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.



回答9:

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.