【发布时间】:2012-11-15 07:32:23
【问题描述】:
我正在尝试使用 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>
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;
?>
非常感谢任何帮助,我从事电子工作而不是编程,我已经为此苦苦挣扎了两天。
【问题讨论】:
-
html文件和php文件在同一个目录
-
什么是“不工作”?是否正在提出请求?您的 JavaScript 控制台中是否有错误?你的
Heatctrl函数正在运行吗? -
P.S.不要在
<script>标签中使用language="javascript"。
标签: php javascript xmlhttprequest