[英]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";
$("#div01").html("<?php echo $target;?>"); } // should be "abc" - doesn't work
它不應該工作。 因為$target
是在test.php中定義的,所以不在javascript .html()
調用的范圍之內。
你可以做:
$("#div01").html(respond);
success:
內部success:
ajax調用的屬性。
另外,在test.php中,我希望您正在執行echo $target
以便將“ abc”推回到blueSky()
函數的respond
對象中
您正在使用AJAX來獲取test.php
,因此您要么必須這樣做:
test.php
$target = 'abc';
echo $target;
或這個:
function blueSky() {
<?php include 'test.php'; ?>
$("#div01").html("<?php echo $target; ?>"); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.