Home > Backend Development > PHP Tutorial > ajax php controls all background function calls, ajaxphp background function_PHP tutorial

ajax php controls all background function calls, ajaxphp background function_PHP tutorial

WBOY
Release: 2016-07-13 09:46:27
Original
763 people have browsed it

ajax php controls all background function calls. The ajaxphp background function

is divided into 3 parts to complete the ajax calling logic of php. The following is the general structure

Part one: ajax request: mainly the action parameter, LoginController is the class name of php, login is the function name in the LoginController class

$('#submit').on('click', function (e) {
    e.stopPropagation();
    $.ajax({
      url: "../../controllers/Controller.php",
      data: {
        action: "LoginController/login",
        username: username,
        password: password
      },
      dataType: "text",
      type: 'POST',
      timeout: 10000,
      error: function () {
        alert("服务器超时");
      },
      success: function (data) {
          alert(data);
      }
    });
  });

Copy after login

Part 2: Controller.php, this file is to call controllers of other specific functional classes and plays a pivotal role, mainly through reflection

<&#63;php

if (!empty($_REQUEST['action'])) {
  try {
    $action = explode('/', $_REQUEST['action']);
    $class_name = $action[0];
    $method_name = $action[1];
    require $class_name . '.php';
    $class = new ReflectionClass($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();
  }
}
&#63;>

Copy after login

The third part: LoginController.php, this file is a specific functional class

<&#63;php
class LoginController {
  
  private $result;
  function LoginController() {
    //初始化数据库连接等参数
  }
  function login($args) {
    //具体的登录逻辑
  }
  function getResult() {
    return $this->result;
  }
}
&#63;>

Copy after login

That’s all the content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1032794.htmlTechArticleajax php controls all background function calls. The ajaxphp background function is divided into 3 parts to complete the ajax calling logic of php. The following is the general structure of the first part: ajax request: mainly act...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template