ajax+php controls all background function calls _php instances

Source: Internet
Author: User

A total of 3 to complete the PHP Ajax call logic, the following is the approximate structure

The first part: AJAX request: The main action 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 ",
      data: {
        action:" Logincontroller/login ",
        Username:username,
        Password:password
      },
      dataType: "Text",
      type: ' POST ',
      timeout:10000,
      error:function () {
        alert ("Server timeout");
      },
      success:function (data) {
          alert (data);
  }});

Part II: controller.php, this file is to invoke other specific functional class controller, play a pivotal role, mainly through reflection to achieve

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

Part III: logincontroller.php, this file is a specific functional class

<?php
class Logincontroller {
  private $result;
  function Logincontroller () {
    //Initializing database connection parameters
  }
  function Login ($args) {
    //Specific login logic
  }
  function GetResult () {return
    $this->result;
  }
}
? >  

The meaning is the whole content of this article, I hope you can enjoy it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.