XMLHttpRequest将变量传递给php脚本
我试图使用XMLHttpRequest将一个变量传递给php脚本,然后让php回显它。我不明白它为什么不起作用,有人能帮我一下吗。 下面是JavascriptXMLHttpRequest将变量传递给php脚本,php,javascript,xmlhttprequest,Php,Javascript,Xmlhttprequest,我试图使用XMLHttpRequest将一个变量传递给php脚本,然后让php回显它。我不明白它为什么不起作用,有人能帮我一下吗。 下面是Javascript <script language="javascript" type="text/javascript"> // Browser Support function Heatctrl(heatMode){ var heatControlRequest; try{ //Good Brow
<script language="javascript" type="text/javascript">
// Browser Support
function Heatctrl(heatMode){
var heatControlRequest;
try{
//Good Browsers
heatControlRequest = new XMLHttpRequest();
} catch (e) {
//Internet Explorer
try{
heatControlRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
heatControlRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e) {
alert("Brower not supported");
return false;
}
}
}
// Function to receive data from server and store in variable
heatControlRequest.onreadystatechange = function(){
if (heatControlRequest.readystate == 4){
alert(heatControlRequest.responseText);
//document.getElementById("controlMode").innerHTML=heatControlRequest.responseText;
//var heatControlResponse = heatControlRequest.responseText;
}
}
var url = "heatControl.php?heatmode="+heatMode;
// Send the request
heatControlRequest.open("GET", url, true);
heatControlRequest.send();
}
</script>
//浏览器支持
函数Heatctrl(heatMode){
var热控请求;
试一试{
//好浏览器
heatControlRequest=新的XMLHttpRequest();
}捕获(e){
//Internet Explorer
试一试{
heatControlRequest=newActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
试一试{
heatControlRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){
警报(“不支持浏览器”);
返回false;
}
}
}
//函数从服务器接收数据并存储在变量中
heatControlRequest.onreadystatechange=函数(){
if(heatControlRequest.readystate==4){
警报(heatControlRequest.responseText);
//document.getElementById(“controlMode”).innerHTML=heatControlRequest.responseText;
//var heatControlResponse=heatControlRequest.responseText;
}
}
var url=“heatControl.php?heatmode=“+heatmode;
//发送请求
heatControlRequest.open(“GET”,url,true);
heatControlRequest.send();
}
HTML
<div id="boilerButtons">
<button type="button" onclick="Heatctrl('On')">Heating On</button>
<button type="button" onclick="Heatctrl('Off')">Heating Off</button>
<button type="button" onclick="Heatctrl('Boost')">Boost</button>
</div>
加热
加热
促进
还有php
<?php
$controlMode = $_GET['heatmode'];
echo $controlMode;
?>
任何帮助都是非常感谢的,我在电子行业工作,而不是编程,我已经为此奋斗了两天 问题在于这一行:
if (heatControlRequest.readystate == 4){
^ this should be an uppercase S
应该是:
if (heatControlRequest.readyState == 4){
从:
XMLHttpRequest对象可以处于多种状态。readyState属性必须返回当前状态
html文件和php文件在同一目录中“不工作”是什么?是否正在提出请求?JavaScript控制台中是否存在错误?您的
Heatctrl
功能正在运行吗?请不要在
标记中使用language=“javascript”
。是的,这很有效,非常感谢。只是出于兴趣,有没有任何原因可以解释为什么这没有在Chrome的javascript控制台中显示为错误?不客气。Javascript允许您检查像这样的对象的未定义属性的值,而不会引发错误。