I'm sendint serialized data to items.php file with AJAX, I'm checking result with FireBug, AJAX send data like this category=2
my items.php file is:
<?php
require_once('db.php');
if (isset ($_POST['category'])) {
$kat = mysql_real_escape_string($_POST['category']);
if ($kat == 0){
$kat = '';
}elseif ($kat == 1){
$kat = 'ORDER BY date DESC';
}elseif ($kat == 2) {
$kat = 'ORDER BY price DESC';
}elseif ($kat == 3) {
$kat = 'ORDER BY price';
}
}else{ $kat = '';}
$query = "SELECT * FROM prekes ".$kat."";
mysql_query("SET NAMES 'UTF8'");
$q = mysql_query($query) or die(mysql_error());
$i = 0;
while($f = mysql_fetch_array($q)){
echo '<div kaina="'.$f['kaina'].'" class="prekes"><img src="prekes/'.$f['foto'].'">'.$f['pavadinimas'].'<br /><strong>'.$f['kaina'].'Lt </strong></div>';
}
?>
and here is my AJAX success function:
$.ajax({
type: 'POST',
url: 'items.php',
data: cleaned,
success: function () {
$('#items').fadeOut("fast").load("items.php?ts="+ $.now()).fadeIn("fast");
}
});
Div refreshes after success, but I'm getting old, unordered result. Why does it happening?
I'm sendint serialized data to items.php file with AJAX, I'm checking result with FireBug, AJAX send data like this category=2
my items.php file is:
<?php
require_once('db.php');
if (isset ($_POST['category'])) {
$kat = mysql_real_escape_string($_POST['category']);
if ($kat == 0){
$kat = '';
}elseif ($kat == 1){
$kat = 'ORDER BY date DESC';
}elseif ($kat == 2) {
$kat = 'ORDER BY price DESC';
}elseif ($kat == 3) {
$kat = 'ORDER BY price';
}
}else{ $kat = '';}
$query = "SELECT * FROM prekes ".$kat."";
mysql_query("SET NAMES 'UTF8'");
$q = mysql_query($query) or die(mysql_error());
$i = 0;
while($f = mysql_fetch_array($q)){
echo '<div kaina="'.$f['kaina'].'" class="prekes"><img src="prekes/'.$f['foto'].'">'.$f['pavadinimas'].'<br /><strong>'.$f['kaina'].'Lt </strong></div>';
}
?>
and here is my AJAX success function:
$.ajax({
type: 'POST',
url: 'items.php',
data: cleaned,
success: function () {
$('#items').fadeOut("fast").load("items.php?ts="+ $.now()).fadeIn("fast");
}
});
Div refreshes after success, but I'm getting old, unordered result. Why does it happening?
Share Improve this question edited Jul 19, 2012 at 14:54 Osvalda Kazlaučiūnaitė asked Jul 19, 2012 at 14:35 Osvalda KazlaučiūnaitėOsvalda Kazlaučiūnaitė 1651 gold badge3 silver badges12 bronze badges 1-
1
You might not want to response with HTML. Try using
json_encode()
. – Amberlamps Commented Jul 19, 2012 at 14:37
4 Answers
Reset to default 4Since load
method uses jQuery get
method internally, I guess you are getting cached data. Make sure to send a unique key as part of the querystring and it will give you the new content /result /uncached data. You may use the Date
object to get create a new unique string;
$('#items').fadeOut("fast").load("items.php?ts="+new Date().getTime())
EDIT : jQuery has a small method called $.now()
to give you a unique timestamp. you may use that instead of writing the Date().getTime() method.
$('#items').fadeOut("fast").load("items.php?ts="+ $.now())
The $.now() method is a shorthand for the number returned by the expression (new Date).getTime().
Thanks Kevin for pointing out. :)
EDIT 2 : After seeing the code
1) You are not passing the category
querystring value which is being used to return the ordered list.You should pass that like this
$('#items').fadeOut("fast").load("items.php?category=2&ts="+ $.now())
2) load
method is a GET
request. in your PHP code(items.php) you are accessing it using $_POST(is that correct ? Shouldnt be $_GET ?
So Change
$kat = mysql_real_escape_string($_POST['category']);
to
$kat = mysql_real_escape_string($_GET['category']);
To prevent any ajax calls from being cached I generally have a global function setup.
(This is function independent, ie post/get/etc)
$.ajaxSetup({
cache: false
});
The issue is .load('items.php')
which uses a GET requests and by design GET requests are cached in the browser so it does not have to keep looking up things. Set the proper headers on the server so the page is not being cached.
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 00:00:00 GMT");
?>
add header no-cache
headers: { 'Cache-Control':'no-cache' }