Jquery load() 和 PHP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8479974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:49:21  来源:igfitidea点击:

Jquery load() and PHP variables

phpjqueryvariablesload

提问by user1091856

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()?

如果我使用 Jquery .load(file.php) 加载 PHP 页面,包含的文件是否可以使用在调用 load() 的页面上定义的 php 变量?

回答by themerlinproject

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

不,您必须将要使用的变量传递给您file.php

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

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

然后你可以在你的 file.php 中获取它们:

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

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

如果有很多变量,则使用 POST 方法:

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

And then get the variables in file.php:

然后获取file.php中的变量:

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

回答by Naftuli Kay

You're misunderstanding how things work.

你误解了事情的运作方式。

  • PHP runs beforeany browser response is issued to the client, and all code runs on the server. The variables declared in your PHP file are destroyed after all the PHP code has been run; they "vanish."
  • JavaScript runs afterthe browser response has begun, and all code runs on the client. By "loading" the output result of the PHP file, you won't get any access to PHP's variables, only the output.
  • PHP向客户端发出任何浏览器响应之前运行,并且所有代码都在服务器上运行。在所有 PHP 代码运行后,PHP 文件中声明的变量将被销毁;他们“消失了”。
  • JavaScript浏览器响应开始运行,所有代码都在客户端上运行。通过“加载”PHP 文件的输出结果,您将无法访问 PHP 的变量,只能访问输出。

If you want to transfer certain variables from PHP to JavaScript, you could dump some output into JSONin your PHP script, like so:

如果您想将某些变量从 PHP 传输到 JavaScript,您可以将一些输出转储到PHP 脚本中的JSON中,如下所示:

<?PHP
    header("Content-Type: application/json");

    $myVariable = "hello world";

    echo json_encode(array(array("myVariable" => $myVariable)));

    /* Output looks like this:
       [
           {
               "myVariable": "hello world"
           }
       ]
    */
?>

Your JavaScript/JSON should look something like this:

你的 JavaScript/JSON 应该是这样的:

$.getJSON("test.php", function(result) {
    console.log(result[0].myVariable);
});

Does that make sense?

那有意义吗?

回答by konsolenfreddy

Yes, use the data parameter, see http://api.jquery.com/load/:

是的,使用 data 参数,参见http://api.jquery.com/load/

$('#someelement').load(
    "test.php", 
    {
        'key1': '<?php echo $value1; ?>',
        'key2': '<?php echo $value2; ?>'
    } 
);

The parameters are posted to the file test.phpand are accessible as:

参数被发布到文件中test.php,并且可以通过以下方式访问:

$_POST['key1']
$_POST['key2']

回答by B.F.

The second argument (params) of the JQuery load function should be an object or a callback function, but could also be an empty string. Depending on that, load does send post or get requests.

JQuery 加载函数的第二个参数 (params) 应该是一个对象或回调函数,但也可以是一个空字符串。根据这一点,load 确实会发送 post 或 get 请求。

I had the idea to switch automatically between get and post, (for example if cookie set),because get is more fast and cache able, and post is more save.

我的想法是在 get 和 post 之间自动切换(例如,如果设置了 cookie),因为 get 更快且可以缓存,并且 post 更节省。

Its worse to write the load function including the content inside the callback function twice than to write something like that:

两次编写包含回调函数内部内容的加载函数比编写类似的内容更糟糕:

//get
var url="cache_dir/my_bag.html";
var params="";
if(document.cookie){
  //post
  url="post.php";
  params="{my:bag}";
  }
$(selector).load(url,params,function(){
...

  });

回答by fardjad

variables scope in the PHPscript loaded by JavaScriptis different from the page that loaded the PHPscript, so the answer is no.

PHP加载的脚本中的变量作用域与加载脚本JavaScript的页面不同PHP,所以答案是否定的。

but you can define global variables or use super global variables like ($_GET, $_POST, etc.) to get what you want.

但你可以定义全局变量或使用像(超级全局变量$_GET$_POST等等),以得到你想要的。

回答by Watermark Studios

You would have to pass those variables to the loaded PHP file through the .load function.

您必须通过 .load 函数将这些变量传递给加载的 PHP 文件。

Example:

例子:

$("#objectID").load("test.php", { 'choices[]': ["{$choice1}", "{$choice2}"] } );

The variables defined in the current PHP file would become part of the Javascript that loads the new PHP file.

当前 PHP 文件中定义的变量将成为加载新 PHP 文件的 Javascript 的一部分。