user1091856
user1091856

Reputation: 3158

Jquery load() and PHP variables

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

Upvotes: 8

Views: 40503

Answers (6)

konsolenfreddy
konsolenfreddy

Reputation: 9671

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

Upvotes: 6

B.F.
B.F.

Reputation: 477

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

  });

Upvotes: 0

Naftuli Kay
Naftuli Kay

Reputation: 91850

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?

Upvotes: 14

fardjad
fardjad

Reputation: 20424

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.

Upvotes: 0

Watermark Studios
Watermark Studios

Reputation: 1183

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.

Upvotes: 0

themerlinproject
themerlinproject

Reputation: 3582

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

Upvotes: 19

Related Questions