Load variables in external php

Asked

Viewed 64 times

0

I have a page that I need to open several modals, not to compromise in loading the page I decided to call the modals 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 with this function any page, but the problem is that there are variables php that need to be considered, as user_id (that comes from a GET), after catching that id I do various treatments like search the user, translate dates, call more user related tables. This is on the main page, but I need this information to also pass to modal efficiently. I’ve already thought about passing the user_id by GET method in AJAX, and copy 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 it? The way I did, it’s the most recommended?

  • Can you return a Json with the php information where the ajax was encoded? I believe you can build a Json with everything you need and in return you can get the values you need.

1 answer

0

I didn’t understand it very well, but if these PHP variables are set ($_GET) on the page you call the modal, just do something like.

<?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!");
    }
  });
}

Browser other questions tagged

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