我在将javascript变量发布到php文件时遇到问题。请有人告诉我发生了什么事吗?
// Get Cookies
var getCookies = document.cookie;
cookiearray = getCookies.split(';');
SelectedIds = cookiearray[0];
//take key value pair
name = cookiearray[0].split('=')[0];
value = cookiearray[0].split('=')[1]; // The variable(values) i want to pass
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
hr.open("POST", url, true);
var url = "page.php";
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("Comp").innerHTML = return_data;
}
}
hr.send(value); // Request - Send this variable to PHP
document.getElementById("Comp").innerHTML = "loading...";
的PHP
$test = $_POST['value'];
print_r($test); // NULL
谢谢
代替
print_r($test);
使用回声
echo $test;
由于$test
不是数组是一个字符串值。print_r
用于打印数组。这就是为什么给出空值的原因。
而且您在ajax中的send函数应如下所示:
hr.send("value="+value);
在send函数中,传递的参数必须是这样的字符串:
"name=value&anothername="+encodeURIComponent(myVar)+"&so=on"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句