簡體   English   中英

javascript以獲取php變量不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM