4

I am trying to pass a variable to a php script using XMLHttpRequest and then have the php echo it back. I can't understand why it isn't working, could someone please help me. Here is the 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>

and the php

<?php
$controlMode = $_GET['heatmode'];
echo $controlMode; 
?>

Any help is much appreciated, I work in electronics not programming and I've been struggling with this now for two days.

3
  • html file and php file are in same directory Commented Nov 27, 2012 at 16:22
  • What's "not working"? Is a request being made? Are there errors in your JavaScript console? Is your Heatctrl function being ran? Commented Nov 27, 2012 at 16:37
  • P.S. Don't use language="javascript" in your <script> tag. Commented Nov 27, 2012 at 16:39

1 Answer 1

3

The problem is this line:

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

It should be:

if (heatControlRequest.readyState == 4){ 

From the docs:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yay that worked, thank you so much. Just out of interest, is there any reason why this didn't show up as an error in Chrome's javascript console?
You're welcome. Javascript allows you to check the value of undefined properties of an object like this without throwing an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.