Jump to content

Recommended Posts

I have found a fairly generic script for refreshing the content of a div without refreshing the whole page and although it works fine, there are a few things I would like to improve on if they are possible.

 

 <script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
	 $("#refreshdiv").load("activity.php");
   var refreshId = setInterval(function() {
      $("#refreshdiv").load('activity.php?randval='+ Math.random());
   }, 9000);
   $.ajaxSetup({ cache: false });
});
</script>
<div id="refreshdiv">
</div>

 

There are, in all, 6 divs that I am looking to refresh. 3 of them show on every page and the other 3 are all on the same page (giving a total of 6 on that page) and my 3 questions are.......

 

1. All of the divs are pulling results from a MySQL db, via Php, and what I have had to do for now is set up a new page for each of the divs content and have them pulled into the requesting page, resulting in 6 new pages. Is there a way that I can keep the content all on the one page and have Ajax refresh the query/result within the div tags instead of loading/reloading an external page.

I assume that the '.load' section may be the key to this. My guess is that it would be possible to write a function that would do this but am not sure where to start.

 

2. At the moment, there are 6 different scripts to allow the refresh of 6 different divs. Is there a way to cover all 6 divs in one script? I would assume that I could name all the divs the same but if question 1 is not achievable, then I will still have to call 6 different pages within the script. I would imagine that I could wrap the whole page in a refreshing div but would guess that is not recommended.

 

3. This may be one for the php forum but I will give it a go anyway. I have been playing around with setting up a page view counter and it works fine with the following code

$stamp = date('Y-m-d H:i:s');
$page = $_SERVER['REQUEST_URI'];
$page_view = mysql_query (" SELECT * FROM page_views WHERE url = '$page' ");
$page_view_match  = mysql_num_rows($page_view);
$page_view_row = mysql_fetch_assoc($page_view);

if($page_view_match > 0)
{
mysql_query("UPDATE page_views  SET member_views = member_views +1, last_member = '".$stamp."' WHERE url = '$page'"  ) or die(mysql_error());
}
else
{
mysql_query ( "INSERT INTO page_views (url, member_views, last_member)  VALUES ( '".$page."', '1', '".$stamp."' )" ) or die(mysql_error()); 
}

 

On first page load, it correctly creates/updates 'path/to/file.php'. On the ajax reload it always adds a new record with a random number like  'path/to/file.php?randval=0.6086456351913512&_=1316338620227'.

I have tried taking out the '?randval='+ Math.random()' part and it left 'path/to/file.php?_=1316338620227' which, I guess, may be a time stamp.

I need it to ignore anything from randval onwards to make sure it only enters the initial page view into the table and not every Ajax reload of a div.

 

I have tried pre-empting the above code with

$path = $_SERVER['REQUEST_URI']; 
$pos = stripos($path, '/randval=/'); 
if ($pos>0) 
{

to try to make it ignore any Ajax reloads but it does not work.

So going back to the original Ajax script, would it be possible to set a counter variable to zero on page load and increment it on any reload? That way, I can tell the php script to only update the page views when the counter is zero. I know this sort of thing would be possible in php alone but as I am having trouble getting php to recognise that I want the 'randval=' part of the code ignored, I am not sure where to go now!

 

Thanks in advance for any help.

Steve

Link to comment
https://forums.phpfreaks.com/topic/247369-refreshing-div-content/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.