4

Hi I have searched the web but can't get this to work. I'm trying to call the file databaseUpdated.php (placed in the same folder as the index.php file) from a function that is called every 10 seconds.

If I place the following in the index.php

<script type='text/javascript'>updateboolean();</script>;

the function is runned so thats not the problem, the problem is that the php file is not read.

the file databaseUpdated.php

<body>
<?php

echo ("<script type='text/javascript'>updateboolean();</script>;");

?>
</body>

And here is my functions in the javascript

 $(document).ready(function(){
        setInterval(function() {
        $.get("databaseUpdated.php");//Can't get this to work any obvious reason for this (And yes I have jquery working)?
        return false;            
    }, 10000);
    });



    function updateboolean(){
        alert("Database updated");
        document.location.reload(true);
    }

Thanks in advance =)

Edit

_________________________________________________________________________________________

When I do

alert(data); in the below function I get the result as the image will show

$(document).ready(function(){
            setInterval(function() {
               $.get('databaseUpdated.php', function(data) {
               alert('Load was performed.');
               alert(data);
               eval(data);
            });
        }, 5000);
   });

enter image description here

But the "eval(data)" doesn't seem to work

5
  • What error did you get in the console? Is there a HTTP request? What is the status code? Commented May 3, 2013 at 12:29
  • So, you have to load this php file every 10 seconds that, in your mind, should call a Javascript function? Why don't you just call the function every 10 seconds instead? Commented May 3, 2013 at 12:32
  • Define "Can't get this to work" - start by verifying that a request is made to that url using your browser (e.g. chrome network tab) - and that the response (which you're ignoring - that's "ok") is not a http error. Commented May 3, 2013 at 12:48
  • I got it, i shoudn't have the html tags etc, now it works, thanks cfor the help =) Commented May 3, 2013 at 13:30
  • there is so much wrong loading a php file which writes a string which you eval which calls a js function - instead of e.g. returning json and if (data.success) { callthisFunction(); } or at the very least using getScript - since that's what you're doing. Commented May 3, 2013 at 14:20

5 Answers 5

3
$.get("databaseUpdated.php"); // This will only return contents of that file The scripts in that file are not executed. To execute them you need to do eval(). So Try This

$.get('databaseUpdated.php', function(data) {
       eval(data);
    });

Also, may be you will require to change your php file as following:

echo ("updateboolean();");
Sign up to request clarification or add additional context in comments.

Comments

3

where is your callback function for ajax this is how it should be

 $(document).ready(function(){
            setInterval(function() {
               $.get('databaseUpdated.php', function(data) {
               alert('Load was performed.');
            });
        }, 10000);
   });

Comments

1

Try this:

$(document).ready(function(){
      setInterval(function() {
           $.get('databaseUpdated.php', function(data) {
           alert("Database updated");
           // or alert(data); //in case you return data from php
           document.location.reload(true);
        });
    }, 10000);
});

Comments

-1

By doing $.get("databaseUpdated.php"), you request the PHP page, but ignore the return results.

Try this:

PHP

echo "updateboolean();";

JavaScript:

$.getScript("databaseUpdated.php");

Comments

-3

try this in place of your echo

 echo "<script>updateboolean();</script>";

Comments

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.