1

I have this code that takes data from my database ajax style . for example 9 at a time, when i want more displayed i press the more button. The problem occurs when i leave the page. When i return even if i use the back button i have to press the more button again to get to where i was.

Is there a way of applying sessions to remember the last count

Javascript on same page as loadmore.php

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{
$('.more').live("click",function()
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="moreajax.gif" />');

$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove(); // removing old more button
}
});
}
else
{
$(".morebox").html('The End');// no results
}

return false;
});
});
</script>

This is the main php script loadmore.php

    <div id='container'>
<ol class="timeline" id="updates">

<?php
include('config.php');
$sql=mysql_query("select * from messages ORDER BY msg_id DESC LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg_id=$row['msg_id'];
$message=$row['message'];
?>
<li>
<?php echo $message; ?>
</li>
<?php } ?>
</ol>

//More Button here $msg_id values is a last message id value.
<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" class="more" id="<?php echo $msg_id; ?>">more</a>
</div>

</div>;

next is the php which is called ajax_more.php

   <?php
include("config.php");
if(isSet($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$lastmsg=mysql_real_escape_string($lastmsg);
$result=mysql_query("select * from messages where msg_id<'$lastmsg' order by msg_id desc limit 9");
while($row=mysql_fetch_array($result))
{
$msg_id=$row['msg_id'];
$message=$row['message'];
?>
<li>
<?php echo $message; ?>
</li>
<?php
}
?>

//More Button here $msg_id values is a last message id value.
<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" id="<?php echo $msg_id; ?>" class="more">more</a>
</div>
<?php
}
?>

if any one can shine any light on this. I would be very greatful

Steve

Steven Kinnear
  • 412
  • 3
  • 19

2 Answers2

0

Javascript has the ability to read/write cookies. For example:

document.cookie =
    'msgCount=9; expires=Sun, 4 Mar 2012 20:47:11 UTC; path=/';

There are some useful javascript functions for creating, reading, and erasing cookies found here: http://www.quirksmode.org/js/cookies.html

There are also some good references here on SO:

javascript cookie, little help please

What is the "best" way to get and set a single cookie value using JavaScript

Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0

I have recently implemented this sort of setup: what I did to get around the problem is put the session setting code into the AJAX script and then, in the main/calling script, use that data to generate the content appropriately whenever you navigate back to that page. If you need to do it in a JavaScript script, make this a PHP script and change the content-type to text/javascript (or whatever the MIME type is supposed to be) and then you can set up the JavaScript source dynamically with PHP, just as you would with HTML... While this approach worked just fine, ultimately I went for a purely dynamic JS solution - without session variables - because my setup never required you navigating away from the same page.

Xophmeister
  • 8,884
  • 4
  • 44
  • 87