[英]Passing JS variable to PHP
I am trying to pass a JS variable to PHP and have PHP echo the JS variable back. 我试图将JS变量传递给PHP,并让PHP回显JS变量。 I keep getting an empty null string.
我不断得到一个空的空字符串。 What am I doing wrong?
我究竟做错了什么?
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);
?>
you should separate php code into a different file and it should be working. 您应该将php代码分成一个不同的文件,并且应该可以正常工作。
if(u){
var dt = {'ud':u};
console.log(dt);
$.post('xrege.php', dt, function(r){
console.log(r);
console.log(typeof r);
},"json");
}
name the above code like 123.html and keep the below code in xrege.php 将上面的代码命名为123.html,并将下面的代码保存在xrege.php中
<?php
$ud = $_POST['ud'];
echo json_encode($ud);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.