Calling a Class via javascript / Jquery

0

Is it possible to load a specific method from an ajax? Let me 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){

    }
}
    
asked by anonymous 17.11.2014 / 12:26

1 answer

0

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

Files:

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

ActionClass.php : Your Action class;

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();               
}
    
17.11.2014 / 12:42