Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 671
user1091856 Avatar asked Dec 12 '11 19:12

user1091856


People also ask

How to use load function in PHP?

The DOMDocument::load() function is an inbuilt function in PHP which is used to load an XML document from a file. Parameters: This function accepts two parameters as mentioned above and described below: $filename: This parameter holds the path to the XML document.

How to get data on page load in jQuery?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element. Syntax: $(selector).load(URL,data,callback);

What is the use of jQuery load method?

The Load() method in jQuery helps to load data from server and returned into selected element without loading the whole page. Syntax: $(selector). load(URL, data, callback);


3 Answers

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'];
like image 137
themerlinproject Avatar answered Oct 11 '22 18:10

themerlinproject


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?

like image 43
Naftuli Kay Avatar answered Oct 11 '22 20:10

Naftuli Kay


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']
like image 24
konsolenfreddy Avatar answered Oct 11 '22 18:10

konsolenfreddy