我正在尝试将JS变量传递给PHP并让PHP回显JS变量。 我一直得到一个空的空字符串。我究竟做错了什么?
function(u){
if(u){
var dt = {'ud':u};
console.log(dt);
$.post('xrege.php', dt, function(r){
console.log(r.responseText);
console.log(typeof(r.responseText));
});
}
});
<?php
$ud = $_POST['ud'];
echo json_encode($ud);
?>
答案 0 :(得分:1)
你应该将PHP代码分成不同的文件,它应该正常工作。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class='showDiv' value="1">1</button>
<button class='showDiv' value="2">2</button>
<button class='showDiv' value="3">3</button>
<div class="layoutGroups" id="layoutGroup1">
<h2>UK Map</h2>
<div div style="width: 650px; height: 700px;"id = "MapDIV"></div>
<div id="userUpdateDIV"></div>
<div id = "BarChartDIV"></div>
<div id="divPack1"></div>
</div>
<div class="layoutGroups" id="layoutGroup2">
<div id= "tree">Tree</div>
</div>
<div class="layoutGroups" id="layoutGroup3">
<div id = "map">Map</div>
</div>
将上述代码命名为123.html,并将以下代码保留在xrege.php
中if(u){
var dt = {'ud':u};
console.log(dt);
$.post('xrege.php', dt, function(r){
console.log(r);
console.log(typeof r);
},"json");
}