Alegro
Alegro

Reputation: 7956

javascript to get php variable doesn't work

<div id="div01">01</div>
<div id="div02">02</div>
<img src="../img/logo.png" onclick="blueSky()"/>

js

function blueSky() {
$.ajax({
type:'GET',
url: 'test.php',
success: function(respond) { 
  document.getElementById("div02").innerHTML=respond;  // works
}
});
$("#div01").html("<?php echo $target;?>"); }   // should be "abc" - doesn't work

test.php

...    
$target = "abc";

Upvotes: 0

Views: 167

Answers (2)

uınbɐɥs
uınbɐɥs

Reputation: 7341

You are using AJAX to get test.php, so you will either have to do this:

test.php

$target = 'abc';
echo $target;

or this:

function blueSky() {
<?php include 'test.php'; ?>
$("#div01").html("<?php echo $target; ?>"); }

Upvotes: 1

raidenace
raidenace

Reputation: 12826

$("#div01").html("<?php echo $target;?>"); }   // should be "abc" - doesn't work

It is not supposed to work. Because $target is defined in test.php and is not within the scope of where you have the javascript .html() call.

You can do:

$("#div01").html(respond); 

inside the success: attribute of your ajax call.

Also, in test.php, I hope you are doing an echo $target in order to get "abc" pushed back into the respond object of blueSky() function

Upvotes: 6

Related Questions