我正在尝试使用XMLHttpRequest将变量传递给PHP脚本,然后让PHP回显它。我不明白为什么它不起作用,有人可以帮我吗?以下是JavaScript代码:
<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>
The 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;
?>
非常感谢任何帮助,我从事电子工作而非编程,这个问题已经困扰我两天了。
Heatctrl
函数是否正在运行? - gen_Eric<script>
标签中使用language="javascript"
。 - gen_Eric