Load variables in external php

0

I have a page that needs to open several modalities, so as not to compromise on loading the page I decided to call the modal only when the user requested it. I did the following AJAX function:

function loadModal(modalPage, modalBox){
$.ajax({
    url: "/modals/" + modalPage,
    success: function(data){
        $("#" + modalBox + " .modal-body").html(data);
    },
    beforeSend: function(){
        $("#" + modalBox + " .modal-body").html("<img src='/images/loading.gif'> Carregando...");
    },
    error: function(){
        $("#" + modalBox + " .modal-body").html("Ocorreu uma erro!");
    }
});
}

So far so good, I can call any page with this function, but the problem is that there are php variables that need to be considered, such as user_id (which comes from a GET), after I get this id I do several treatments like searching for the user, translating some dates, calling some user related tables. This is on the main page, but I need this information to go to modal too efficiently. I already thought about passing user_id by GET method in AJAX, and copying all that treatment I do on the main page. How to make modal read variables without having to do this? Or do I need to do this? The way I did it, is it the most recommended?

    
asked by anonymous 29.07.2016 / 01:06

1 answer

0

I do not understand very well, but if these PHP variables are defined ($ _GET) on the page that you call modal, just do something like this.

<?php 

$data = array('user_id' => $_GET['user_id']); 

?>

function loadModal(modalPage, modalBox) {
  
  var data = <?php echo json_encode($data); ?>;
  
  $.ajax({
    url: "/modals/" + modalPage,
    data: data,
    success: function(data) {
      $("#" + modalBox + " .modal-body").html(data);
    },
    beforeSend: function() {
      $("#" + modalBox + " .modal-body").html("<img src='/images/loading.gif'> Carregando...");
    },
    error: function() {
      $("#" + modalBox + " .modal-body").html("Ocorreu uma erro!");
    }
  });
}
    
29.07.2016 / 15:56