简体   繁体   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 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 test.php

...    
$target = "abc";
$("#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. 因为$target是在test.php中定义的,所以不在javascript .html()调用的范围之内。

You can do: 你可以做:

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

inside the success: attribute of your ajax call. success:内部success: ajax调用的属性。

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 另外,在test.php中,我希望您正在执行echo $target以便将“ abc”推回到blueSky()函数的respond对象中

You are using AJAX to get test.php , so you will either have to do this: 您正在使用AJAX来获取test.php ,因此您要么必须这样做:

test.php test.php

$target = 'abc';
echo $target;

or this: 或这个:

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