使用GET方法不会传输值

Whenever I write echo $_GET['sec']; then it shows the value of sec but when I try the following code:

    $(document).ready(function() {
        setInterval(function () {
            $('#div_id').load('../data.php?id_to=<?php $_GET['sec'];?>')
        }, 100);
    });

The value of "sec", which is coming from another page does not transfer to data.php with id_to variable.

What's wrong with my code?

I can see the value of $_GET['sec']; in current page but the value is not available on the data.php file.

Code is okay so variable if available should be passed

So what can you try is:

You have simple mistake you don't echo

change <?php $_GET['sec'];?> to <?php echo $_GET['sec'];?> or <?= $_GET['sec'] ?>

also:

  1. what's the output of this php code is it ile '../data.php?id_to=VALUE' If output is okay then variable will be passed via GET method

  2. In your data.php try doing echo $_GET['id_to'] maybe you try to output sec and this causes problem?

  3. You can always try print_r($_GET); on data.php

You're not outputting the GET variable into your JS string.

Replace:

$('#div_id').load('../data.php?id_to=<?php $_GET['sec'];?>')

With:

$('#div_id').load('../data.php?id_to=<?php echo $_GET['sec'];?>')
//                         That was missing ^

Alternatively, there's a shorthand available for a php echo:

$('#div_id').load('../data.php?id_to=<?= $_GET['sec'];?>')

While the syntax highlighter on here may make it seem like the nested single quotes will interfere, they won't. <?= $_GET['sec'];?> will literally be replaced by the value in sec, resulting in a valid JS string.