I loaded a content into a div using php get_file_contents,
now i want to refresh it using ajax but i can’t figure how to do it.
Sample code:
<script>
function refreshmydiv(){
...
}
</script>
<div id="mydiv"> <? echo nl2br(htmlspecialchars($myfile)); ?> <div>
<a href="#" onclick="refreshmydiv();">Refresh My Div</a>
So, here is one way of doing it. First, the parts:
myrefreshfunctionThis function needs to make an AJAX call to
refresh.phpor another page. Then, it should replace the contents ofmydivwith the html that is sent back.refresh.phpThis page needs to return the HTML for the div. It doesn’t need to return the whole page, it only needs to return the contents of the div.
In this case, it would just echo
get_file_contentsand nothing else.ex.
Then, the refresh process looks like this:
Your user presses the button to refresh the div.
Your function requests a page.
The page returns ONLY the contents of the div.
Your function replaces the content of the div with the page it just requested.
There are other ways to do this, put this is a very straightforward way to do it.
If you use jQuery, your
myrefreshfunctionis basically one line of code: