0

There are pages of user profiles that work on the principle of get_file_content. Every time it is generated. I would not like to touch on the structure of the site and decided to do block ui with preloader. I figured it out, but the problem arose. The page is generated on request via $id = $_GET['id'] (player.php?id=123). The script I want to use via the preloader is in another file (stats.php), because preloader works through AJAX Post + $(document).ready(function();, the $id variable cannot be passed from player.php to stats.php) How to be in this case?

Js output + preload:

function show_stats() {
    jQuery.ajax({
        type:"post",
        url: "stats.php",
        beforeSend: function() {
            $('#stats-block').block({ 
                message: '<div class="la-ball-pulse la-dark"><div></div><div></div><div></div></div>',
                css: {
                    border: 'none', 
                    backgroundColor:'transparent'
                } 
            });
        },
        success: function(data) {
            $('.chatonline').show();
            $('#stats-block').unblock();
            $('#stats-moders').html(data);
        },
    });
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Ronohx
  • 71
  • 6
  • 1
    Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) –  Aug 29 '18 at 22:39

1 Answers1

1

Send the variable in the data: option of $.ajax()

function show_stats() {
    jQuery.ajax({
        type:"post",
        url: "stats.php",
        data: { id: <?php echo $id; ?> },
        beforeSend: function() {
            $('#stats-block').block({ 
                message: '<div class="la-ball-pulse la-dark"><div></div><div></div><div></div></div>',
                css: {
                    border: 'none', 
                    backgroundColor:'transparent'
                } 
            });
        },
        success: function(data) {
            $('.chatonline').show();
            $('#stats-block').unblock();
            $('#stats-moders').html(data);
        },
    });
}

stats.php can then get the ID from $_POST['id'].

Another option is to set a session variable. player.php can get $_SESSION['id'] = $id;, then stats.php can read $_SESSION['id'].

If you need to ensure that the ID is the same in both scripts, the session variable is better. The user can easily change the ID in the Javascript.

If there's a possibility that $id could be something other than a simple number, you should encode it to ensure proper syntax:

data: { id: <?php echo json_encode($id); ?> },
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I'll try. Thank you for your prompt response! – Ronohx Aug 29 '18 at 22:25
  • 1
    If `$id` is a simple integer or something and will always have a value, should be fine. But to make it more bulletproof, you could do a `json_encode` on the variable to be sure you'll always end up with valid javascript. This is especially helpful with strings, since it handles escaping quotes and stuff for you. – David784 Aug 29 '18 at 22:36
  • From the example he gave, it looks like it should always be an integer, so the added generality is not necessary. – Barmar Aug 29 '18 at 22:48
  • @Barmar IMHO it's always *best & therefore necessary* – Roko C. Buljan Aug 29 '18 at 23:06
  • @RokoC.Buljan I disagree that it's always necessary, but I've added information about this to the answer. – Barmar Aug 29 '18 at 23:09