Skip to content
Advertisement

Auto Refresh a div in PHP and AJAX

I’m stuck with my auto refresh function in my web course assignment. I always put my code like this :

navigation.php :

JavaScript

index.php :

JavaScript

footer.php :

JavaScript

My ajax code :

JavaScript

auto_refresh_data.php :

JavaScript

I want to make an auto refresh data in the middle of my container. Then I made it in ajax like this : How do i auto refresh my div class content? . It failed because of some php variable, style, js, function, etc placed before the auto refresh div element. So the question is how to make it my data become auto refresh if my code design is like that?

Thankyou

Advertisement

Answer

I think there are a couple things you should be doing to fix this, but the main problem are your functions:

1) Move your functions somewhere else, like into their own files:

/functions.php

JavaScript

2) Include the functions in the main file where it’s used on load:

/index.php

JavaScript

3) On the auto_refresh_data.php page, you need to include the functions again, but use include_once instead of include so you don’t get a declaration error:

JavaScript

4) Since you aren’t really doing anything special with the ajax, I would just use .load():

JavaScript

Further to this, I would probably look at using setTimeout() recursively vs setInterval() just because if you get a delay in response, you may get those responses out of time. Using setTimeout will fire after the response is returned as opposed to every 5 seconds whether the response has come back yet or not.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement