Jquery load() and PHP variables

前端 未结 6 845
死守一世寂寞
死守一世寂寞 2020-12-03 00:29

If I load a PHP page with Jquery .load(file.php), can the included file use the php variables that were defined on the page that called the load()?

6条回答
  •  隐瞒了意图╮
    2020-12-03 00:59

    No, you have to pass the variables you want to use to your file.php:

    $('#yourdiv').load('file.php?var1=xyz&var2=xyz&var3=xyz');
    

    And then you can GET those in your file.php:

    $var1 = $_GET['var1'];
    $var2 = $_GET['var2'];
    $var3 = $_GET['var3'];
    

    If there are a lot of variables then use the POST method:

    $('#yourdiv').load('file.php', {var1:x, var2:y, var3:z})
    

    And then get the variables in file.php:

    $var1 = $_POST['var1'];
    $var2 = $_POST['var2'];
    $var3 = $_POST['var3'];
    

提交回复
热议问题