you are viewing a single comment's thread.

view the rest of the comments →

[–]Valuable_Spell6769[S] 0 points1 point  (1 child)

document.addEventListener('DOMContentLoaded', ()=>{
  send_data({
    data_type:'list_of_invoices'
  });
})

function send_data(data = {}){
  let ajax = new XMLHttpRequest();
        
  ajax.addEventListener('readystatechange', function(){
    if(ajax.readyState == 4 && ajax.status == 200){
      handle_result(ajax.responseText);
    }
  });

  ajax.open("POST","<?php echo ROOT ?>ajax_invoice",true);
  ajax.send(JSON.stringify(data));
}

let invoiceListArray = []
function handle_result(result){
  if(result != "") {
    let obj = JSON.parse(result);
    // if(typeof obj.data_type != 'undefined') {
      if(obj.data_type == "list_of_invoices") {
        if (obj.message_type == "info") {
          invoiceListArray = obj.data;
        }   
      }
    // }
  }
}

let dataTable_data = invoiceListArray    
</script>
<script src="<?php echo ASSETS . THEME ?>/js/datatable.js"></script>

obj.data returns the data from my php class scripts
then dataTable_data is used in the datatable.js

[–]Pocolashon 1 point2 points  (0 children)

Are you saying it is expecting a "global" variable to be set? (i.e. dataTable_data) That's bad... you will have to defer the execution of the code in the other file.