Jquery Load() and PHP Variables

Jquery load() and PHP variables

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?

Load a variable from PHP file using jQuery Ajax load()

You're close, you need to echo the variable in the PHP file -

<?php
$variable = "foo";
echo $variable;
?>

Then load like this -

$('#loadme').load('example.php');

The content of #loadme will now be 'foo'.

EDIT: Based on the OP's comments -

Each echo containing a variable could be a div with an id, e.g.,

echo '<div id="foo">$variable</div>';

Then your load statement would look like this -

$('#loadme').load('example.php #foo');

Now #loadme would contain the variable echoed in that div. This is not an efficient plan if the variables are to be re-used by JavaScript/jQuery code.

Passing Php Variables With jQuery load() function

The selector portion of load() url needs to be part of the url string with a space separator. You are passing it as a separate argument

Try:

$("#bbpress-forums").load(bpFullUrl + ' ' +  bpData);

See example in load() docs under section "Loading Page Fragments"

$( "#result" ).load( "ajax/test.html #container" );

JQuery .load passing PHP variables

jQuery selectors are called with $("#div1").load( instead of $.("#div1").load(. Note the extra period.

PHP cannot access variable declared in loaded php page with jquery load()

PHP is a server-side language while JavaScript is a client-side language. The only way you can get PHP data into JavaScript is by outputting the data in some manner. If you want the full output, just echo it. If you want to load a bunch of simple PHP variables into JavaScript, you can add all the variables you need JS to know about into an array, and then json_encode them.

$firstName = 'John';
$lastName = 'Doe';
$outputArray = array('firstName' => $firstName, 'lastName' => $lastName);
echo json_encode($outputArray);

Then when an ajax function can retrieve this data as an object and you can use result.firstName in JavaScript.

Passing complex items is impossible though, such as those with database connections as properties.

How To Pass PHP Variables On jQuery Load?

In your <body> you can add a hidden field

<input type="hidden" value="<?=$username?>" id="username" />

and in your jquery,

$('a.manage-content-link').click(function (e) {
var self = $(this),
file = self.siblings('input[type="hidden.block-hidden-input"]').val();
var username = $("username").val(); //use this variable where ever you want
var ids = $(this).attr('id'); // this is the id
self.next(".manage-content-wrap").find(".manage-content").load("file-" + file + ".php?id="+ids+"&username="+username); //and in php file usee $_GET
e.preventDefault();
});

Passing variable from PHP file to php file using Jquery

The passing part can happen in the script where the variable is defined, so in file1.php. Then you get the following files:

file1.php:

<?php
$user_rank = 123;
?>
<script>
function getRank()
{
$.ajax({
type: "GET",
url: "file2.php?user_rank=<?php echo $user_rank; ?>",
success: function(result){
$("#TargetRank").html(result);
}
});
};
</script>

file2.php:

<?php
$user_rank = $_GET['user_rank'];
echo $user_rank;
?>

jquery .load passing data as array in URL and trying to recover this array in php

Try this :

var data = { id: 67, nome: 'Augusta Ap Raymo Longo'};
$('.myform').load( "View/editEntregador.php", data, function() { });

If your data is coming from $_POST then you will need to make a foreach loop to generate the data array like the example above.

Example :

echo 'var data={};'."\n";
foreach($_POST as $key => $value){
echo "data.".$key."='".addslashes($value)."';\n";//addslashes to escape possible slashes in your string
}
echo '$(\'.myform\').load( "View/editEntregador.php", data, function() { });';

jquery load pass variable to php file

Just for the update: This task was solved here

I forgot to put this code ( ) in ''

It works like this:

$("#div2").load('loadTable.php', { 'videoId': '<?php echo $video_id ?>'  } );

Javascript/jquery .load passing variable

When you do this:

LoadMyPhpScript2('?MyVar1=0&MyVar2=1')

You are executing the function and passing the result to setTimeout

Try this:

setTimeout(function() { LoadMyPhpScript2('?MyVar1=0&MyVar2=1'); },5000); 

However, sending the url querystring like that is an odd way of doing it. Something like this might be better:

function LoadMyPhpScript2(myVar1, myVar2)
{
var strURL = 'hello.php';
$('#MyDiv2').load(strURL, { myVar1: myVar1, myVar2: myVar2 });
}
setTimeout(function() { LoadMyPhpScript2(0, 1); },5000);


Related Topics



Leave a reply



Submit