Jquery load() and PHP variables

允我心安 提交于 2019-11-26 16:02: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()?


回答1:


You're misunderstanding how things work.

  • PHP runs before any 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 after the 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.

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

<?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:

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

Does that make sense?




回答2:


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'];



回答3:


Yes, use the data parameter, see 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.php and are accessible as:

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



回答4:


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

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.




回答5:


variables scope in the PHP script loaded by JavaScript is different from the page that loaded the PHP script, so the answer is no.

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




回答6:


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.

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.

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(){
...

  });


来源:https://stackoverflow.com/questions/8479974/jquery-load-and-php-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!