Reprint Please specify source: http://www.cnblogs.com/ghypnus/p/4645873.html
It's been a long time since I posted the code here.
A total of 3 to complete the AJAX call logic of PHP, the following is the approximate structure
The first part: AJAX request: The main action of this parameter, Logincontroller is the class name of PHP, login is logincontroller the function name in this class
$ (' #submit '). On (' click ', function (e) {e. Stoppropagation (); $.ajax ({URL: "). /.. /controllers/controller.php " "Logincontrol Ler/login " Username:username, Password:password }, DataType: "text" ' POST ' timeout: 10000 function " Server timed out " function (data) {alert (data); } }); });
Part II: controller.php, this file is called the controller of other specific function classes, play a pivotal role, mainly through reflection to achieve
<?PHPif(!Empty($_request[' Action '])) { Try { $action=Explode(‘/‘,$_request[' Action ']); $class _name=$action[0]; $method _name=$action[1]; require $class _name. '. php '; $class=NewReflectionclass ($class _name); if(class_exists($class _name)) { if($class->hasmethod ($method _name)) { $func=$class->getmethod ($method _name); $instance=$class-newinstance (); $func->invokeargs ($instance,Array($_request)); $result=$instance-GetResult (); Echo $result; } } } Catch(Exception $exc) { Echo $exc-gettraceasstring (); }}?>
Part III: logincontroller.php, this file is specific to the functional class
<? php class Logincontroller {
private $result;
function Logincontroller () { Span style= "color: #008000;" >// initialize parameters such as database connection " function Login ( $args ) { // " function GetResult () { return $this ->result; }} ?
Ajax + PHP + Controller controls all background function calls