加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

XMLHttpRequest将变量传递给php脚本

发布时间:2020-12-13 16:48:34 所属栏目:PHP教程 来源:网络整理
导读:我试图使用 XMLHttpRequest将变量传递给php脚本,然后让php回显它.我无法理解为什么它不起作用,有人可以帮助我. 这是 Javascript. script language="javascript" type="text/javascript" // Browser Support function Heatctrl(heatMode){ var heatControlReq
我试图使用 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; 
?>

我非常感谢任何帮助,我从事电子工作而不是编程工作,而且我现在已经为此困难了两天.

解决方法

问题是这一行:

if (heatControlRequest.readystate == 4){
                            ^ this should be an uppercase S

它应该是:

if (heatControlRequest.readyState == 4){

从docs:

The XMLHttpRequest object can be in several states. The readyState attribute must return the current state.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读