• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

Refreshing DIV in PHP

Question

samg    2

Right, im trying to get a div to auto refresh on my webpage.

Here is the code

  <div class="Box">
  <h2>Forum Stats</h2>
  <?php include 'php/forumstats.php';?>  </div>

This sits in index.php

I've tried over 10 different guides on the net and none work!

Share this post


Link to post
Share on other sites

11 answers to this question

Recommended Posts

  • 0
Mathachew    149

Having your page refresh has nothing to do with PHP. Can you elaborate on exactly what you're trying to do? You're including a file which could have anything in it, and the result will be your coded HTML plus whatever is in php/forumstats.php in your browser. Are you wanting to have your page refresh after a certain number of seconds? Are you wanting to content to automatically update without the page actually refreshing?

Share this post


Link to post
Share on other sites
  • 0
samg    2

The include just pulls out stats from a DB. such as last post etc. Obviously this updates when you refresh the page.

Im not saying it has anything to do with PHP, its either JS or ajax. But i need a script to either refresh a div or a php include.

I'd like to be able to set it to auto refresh just this div (which in turn reloads the include) every X seconds.

Can talk on MSN if you are willing to help? I'dd add you if you ok it!

Share this post


Link to post
Share on other sites
  • 0
raskren    0
The include just pulls out stats from a DB. such as last post etc. Obviously this updates when you refresh the page.

Im not saying it has anything to do with PHP, its either JS or ajax. But i need a script to either refresh a div or a php include.

You can't refresh a DIV, nor any other HTML element "on the fly" without any scripting.

You CAN refresh the entire page using a script or the meta refresh element.

You can also have a script get data from a server and have it write this data to the page at set intervals. This is also known as AJAX.

Share this post


Link to post
Share on other sites
  • 0
samg    2
You can't refresh a DIV, nor any other HTML element "on the fly" without any scripting.

You CAN refresh the entire page using a script or the meta refresh element.

You can also have a script get data from a server and have it write this data to the page at set intervals. This is also known as AJAX.

Fine, im incorrect with the process.

Include outputs figures. I would like said figures to refresh on the page without loading the whole lot! If that is ajax, then let me use ajax. Just tell me how! lol

Share this post


Link to post
Share on other sites
  • 0
Kevin Leclair    0

You could use an iframe, put the src to your php page. Have the php page do a meta refresh. In the end you will have an iframe that will refresh the php page. Make sense, the current solution is only setup to meta refresh in IE.

Embed this in the page where you want to show the contents of the php page:

<iframe width="468" height="60" src="http://clients.seadoosportboats.com/vendors.php" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>

Make your php page look something like this:

<html>
<head>
<script language="javascript">
<!--
if (navigator.appName == "Microsoft Internet Explorer")
document.write('<META HTTP-EQUIV=Refresh CONTENT="30" />');
-->
</script> 
</head>
<body bgcolor="#FFFFFF" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<table border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>
<a href='http://clients.seadoosportboats.com/clientserver/www/delivery/ck.php?n=a770acb9&cb=1002' target='_blank'>
<img src='http://clients.seadoosportboats.com/clientserver/www/delivery/avw.php?campaignid=5&source=SeaDooSportBoats.com&cb=1002&n=a770acb9' border='0' alt='Salt-Away Products, Inc.' title='Salt-Away Products, Inc.' /></a>
</td>
</tr>
</table>
</body>
</html>

I have coded it so it will only meta refresh with IE, you could change this if you would like. See if this helps at all. :)

Share this post


Link to post
Share on other sites
  • 0
samg    2

Thanks! I'll try it now.

What about other browsers too!

Share this post


Link to post
Share on other sites
  • 0
rson451    0

Just search for a simple ajax script. This is the whole point of AJAX, refresh only the part of the page you wish to refresh. The timer can be done in the javascript to launch the request to the server, but worry about that part last. If you don't want to get your hands dirty with some simple ajax, there are plenty of ajax frameworks you could use instead.

Share this post


Link to post
Share on other sites
  • 0
samg    2
Just search for a simple ajax script. This is the whole point of AJAX, refresh only the part of the page you wish to refresh. The timer can be done in the javascript to launch the request to the server, but worry about that part last. If you don't want to get your hands dirty with some simple ajax, there are plenty of ajax frameworks you could use instead.

I'd love to see one then!

I've tried loads of ajax guides and prebuild code and none of them work.

So if you have one that does, please let me know

Share this post


Link to post
Share on other sites
  • 0
Kevin Leclair    0
Thanks! I'll try it now.

What about other browsers too!

Like I said you should be able to modify it, you just need to do a little reading on your own. Read about navigator.appName.

<script language="javascript">
<!--
if (navigator.appName == "Microsoft Internet Explorer")
document.write('<META HTTP-EQUIV=Refresh CONTENT="30" />');
-->
</script>

Let us know what approach you take and if you get it working.

Share this post


Link to post
Share on other sites
  • 0
jimbo11883    1

Check out a library like jQuery, which I use extensively at work and for my own sites. Read up on the library's documentation. If you don't know javascript, now would be a good time to learn, it's quite an easy language to pick up.

In jQuery, it's simple as using this code on your main page.

<head>
  <script type="text/javascript" language="javascript" src="js/jQuery.js"></script>
  <script type="text/javascript" language="javascript">
	function updateForumStats() {
	  $.get("php/forumstats.php", function(data) {
		$("#forumstats").html(data);
		  });
   	  window.setTimeout("updateForumStats();", 10000);
	}

	$(document).ready(function() {
	  updateForumStats();
	});
  </script>
</head>

You can play with the "10000" (the 2nd argument of the window.setTimeout() function, which is in milliseconds) to have it refresh as often as you'd like.

Edited by jimbo11883

Share this post


Link to post
Share on other sites
  • 0
samg    2

Thanks for the help guys. I'm using the iframe for the moment, not idea but it does the job. I'm going to look into learning javascript.

You need to use target="_parent" for it to open in the page not the iframe too.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.