E
E
eSelf2015-11-30 15:25:42
PHP
eSelf, 2015-11-30 15:25:42

How to implement an AJAX request?

I am writing a plugin for CMS Zencart and have come to a dead end.
I need to send an AJAX request to the current page.
I have two files in php

require('includes/application_top.php');
require('includes/mymodule/myModule.php');
//тут остальной код, но не в нем суть

In the js itself, I send the request.
$("input[name='syncProducts']").click(function(event) {
  var myData={};
  myData['action'] = "sync";
  $.ajax({
    url:ajaxURL,
    type:"post",
    data: myData,
    dataType:"json",
    success: function(data){
      console.log(data);
    }
  });
});


Now what's the problem.
The application_top.php file is a standard ZenCart file, in which all configs, paths, etc. He, in turn, loads another dozen files.
In my myModule.php file, a class is implemented for the plugin to work, and in it I see what request came in:

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $dolibarr = Dolibarr::getInstance();
    $action = $_POST['action'];
    switch($action) {
        case 'connect' : 
        break;
        case 'sync' : $dolibarr->syncProducts();
        break;
    }
    die();
}

class Dolibarr{
//тут сам класс

And when I send an AJAX request, it returns the data from application_top.php to me, because he connects first. If it is connected after myModule.php, then those functions that are implemented in application_top.php do not work in my plugin (the same work with the database).
It's confusing, but I don't know how to explain it better.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Facetrollex, 2015-12-01
@Facetrollex

Добавь еще один параметр в ajax запрос и по нему проверяй.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question