Rob Sealey
Rob Sealey

Reputation: 131

How to stop the results of a jquery php load being cached

I have a new problem which MUST affect practically every person who uses AJAX. I have a HTML/JavaScript page which (using a jQuery call) loads a .php into my page - but it seems to cache the results and often just returns the cached information rather than re-running the PHP script.

The code below is how I'm trying to overcome the problem (thanks bazmegakapa for the code - I've just tried to add the cache:flase bit - but it doesn't seem to work).

$(document).ready(function() {
    cache: false
    $('#selecthere').load('/temp/php_script.php', function () { //callback
        $('select', this).change(function () { //catch onchange event of select
            //call your function here, pass the selected value
            initialize($(this).val());
        });
    });
});`

Upvotes: 1

Views: 321

Answers (3)

Richard Dalton
Richard Dalton

Reputation: 35793

It looks like you are doing the cache: false wrong.

$(document).ready(function() { 
    $.ajax('/temp/php_script.php', 
       { 
           cache: false, 
           success: function(result) {
               $('#selecthere').html(result);
           }
       }
    );
});

Upvotes: 0

GordonM
GordonM

Reputation: 31730

Your php_script.php file should send the correct headers to prevent caching.

<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

Upvotes: 3

JohnP
JohnP

Reputation: 50019

Just add a random number to the end

var random = Math.random() * 10;
$('#selecthere').load('/temp/php_script.php?r=' + random, function () { 

Upvotes: 1

Related Questions