0

Hi I have this sample code to load random quotes.

PHP Code:

<?php
  //Selecting Random quotes from database --randomquotes.php
  require('dbconnection.php');
  // $temp=10;
  $sql = "SELECT * FROM db.q order by rand() LIMIT 1";
  $res = mysql_query($sql,$con);
  while ($res1=mysql_fetch_assoc($res))
  { 
    print "<em>".$res1['quote']."</em>";
  }
?>

HTML CODE:

<div class="class_box_shadow_quote">
<?php
  require('randomquotes.php');
?>
</div>

Jquery Code:

<script>
  var $j = jQuery.noConflict();
  $j(document).ready(function(){
    setInterval(function() {
      $j(".class_box_shadow_quote").load('randomquotes.php');
    }, 7000);
  });
</script>

This code is working well for the first time , but is getting stuck on second quote and is not changing the quote. Also everytime i see the second quote to be the same always. What is the problem . Please help?

2 Answers 2

2

I believe the output is being cached by jQuery. You can try with caching disabled.

setInterval(function() {
    $j.ajax({
        url: 'randomquotes.php',
        cache: false,
        success: function(data) {
            $j(".class_box_shadow_quote").html(data);
        }
    });
}, 7000);
Sign up to request clarification or add additional context in comments.

Comments

0

Try adding a random seed at the end of your URL to avoid browser caching, like so:

    $(".class_box_shadow_quote").load('randomquotes.php?s=' + (Math.Random() * 1000000))

3 Comments

Wow this is new to me...but the previous solution was easy to avoid caching .
Sometimes, manipulating the cache headers don't work. I've run into that problem several times. It's getting better though. :)
Actually cache: false does the same. It adds a randomizer to the url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.