0

The function called by onLoad is called but stops before calling another function. I'm sure this is what is happening because I added some debugging document.write() functions.

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function getTemp(){
    document.write("3");//debugging
    $.ajax( {
        type: "GET",
        url: "gettemp.php",
        success:function(data){
            document.write(data);
            document.write("4"); //debugging
        }
    });
}

</script>
</head>
<body onload="onLoad();">
<script>
function onLoad(){
    document.write("1"); //debugging
    getTemp();
    document.write("2"); //debugging
    setInterval(function(){getTemp();}, 10000);
}
</script>
</body>
</html>

It outputs only "1", nothing else. Therefore, I am certain it stops before calling getTemp(). If necessary I will add the php file. As for the split scripts, if you put the onLoad() script in the head script you end up with nothing, not even the "1".

EDIT: Now it gets to "132", but it seems it isn't calling the PHP correctly.

PHP source:

<?php
getTemp();
function getTemp()
{
        $filename = "temp";
        $f = fopen($filename,"r");
        $value = fgets($f);
        $value = $value / 100;
        echo $value;
}
?>
12
  • what does the console say? Commented May 14, 2014 at 12:39
  • 1
    Are there any console errors? What happens when you step through the code in the browser's debugger? Commented May 14, 2014 at 12:39
  • Can you create other functions and see if you can get to them? Commented May 14, 2014 at 12:39
  • 2
    You have an extra brace after the getTemp function declaration. Remove it! Commented May 14, 2014 at 12:40
  • 1
    @user3433131 What do you mean "considering it's run on an Apache webserver, nothing"? The JavaScript console. It will show you your JS error. Commented May 14, 2014 at 12:41

1 Answer 1

6

looks like typo

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function getTemp(){
        document.write("3");//debugging
        $.ajax( {
            type: "GET",
            url: "gettemp.php",
            success:function(data){
                document.write(data);
                document.write("4"); //debugging
            }
        });
    }
// } <-- not needed

</script>
</head>
<body onload="onLoad();">
<script>
function onLoad(){
    document.write("1"); //debugging
    getTemp();
    document.write("2"); //debugging
    setInterval(function(){getTemp();}, 10000);
}
</script>
</body>
</html>

i use console.log('foobar') for debugging :)

EDIT

Its calling the php correctly...

your problem is your php source... you allways call the same line...

your funaction calls the getTemp and the getTemp allways reads the first line...

take a look at the sample for the fgets function

SEE SAMPLE FOR fgets

missing a while or any kind of loop and the file length param :D

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

8 Comments

That worked. Sort of. Now I'm getting all the way to "132". Looks like it isn't calling the php correctly. Should I make a new issue or continue on this one?
what is the source of the php, please edit your post
@user3433131 You should ask a new question; it's now a "How come echo isn't writing back to the client?" (Because that's not what echo does.)
It is the way I do it, I'm using AJAX.
see my edits. echo is fine but it seems like you missing some in your php file
|

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.