Calling a Class by javascript/Jquery

Asked

Viewed 1,045 times

0

Is it possible to load a specific method from an ajax ? I’ll give you an example:

var actions  = {
    options : {
        action  : "newPost" ,
        dados   : {
                  title     : "Novo Post"   ,
                  content   : "Conteúdo"  ,
                  author    : "Autor" 
        }
    },
    init : function() {
        var _this = this;
        $.ajax({
            url         : "action.php" ,
            method      : "POST" ,
            dataType    : "json" ,
            date        : _this.action ,
            success     : function(action){
                console.log(action);
            }
        });
    }
}
action.init();



class Action{
    public $_action;

    public newPost($title , $content , $user){

    }
}

1 answer

0


A solution would be a specific address that calls a specific method you need. Example:

Archives:

index.php: where we will make the call ajax;

ActionClass.php: Your Class Action;

action.php: File where the action will be executed;

//index.php
var actions = {
  options: {
   action: "newPost",
    dados: {
      title: "Novo Post",
      content: "Conteúdo",
      author: "Autor"
    }
  },
  init: function() {
    var _this = this;
    $.ajax({
      url: "action.php",
      method: "POST",
      dataType: "json",
      date: {action:_this.action},
      success: function(action) {
        console.log(action);
      }
    });
  }
}
action.init();
//ActionClass.php
class Action{
    public $_action;

    public newPost($title , $content , $user){

    }
}
//action.php
include "ActionClass.php";

$action = new Action();

$act = $_POST['action'];

if($act == 'newPost'){
  $action->newPost([...]);
}else if($act == 'deletePost'){
  $action->deletePost();               
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.