Reputation: 440
I am displaying data from database by Ajax and I need to add countdown timer with times from database. Is it possible to do it as I am struggling with it for about 24 hours. You can look at main code here http://www.egrupper.pl and as you will see the countdown timers are not working. The AJAX code:
function showDeals(sid,limit,cat)
{
sid=typeof sid !== 'undefined' ? a : 1;
limit = typeof limit !== 'undefined' ? limit : 0.7;
if(sid=="" || limit==""){
document.getElementById("con").innerHTML="";
return;
}
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("con").innerHTML=xmlhttp.responseText;
$('#con').masonry({
itemSelector: '.clsDeal_Blk_Cont'
});
}
}
xmlhttp.open("GET","getdeals.php?sid="+sid+"&limit="+limit+"&cat="+cat,true);
xmlhttp.send();
}
and in getdeals.php I have countdown timer code which looks like this:
$(document).ready(function(){
$("#countdown_<?php echo $id; ?>").countdown({
date: "date_from_database",
format: "on"
},
function() {
// callback function
});
});
thanks @user2008945 for help which was useful for me, but anyway I need more than one countdown timers on page and I though that something like this will do but unfortunatelly not:
success:function(rows){
for (var i in rows){
var row=rows[i];
var id=row[0];
var end_date=row[1];
$("#countdown_"+id).countdown({
date: end_date,
format: "on"
},
function() {
// callback function
});
}
}
and
$data=array();
while($row=mysql_fetch_row($result))
{
$data[]=$row;
}
die (json_encode($data));
Upvotes: 0
Views: 1119
Reputation: 1754
Unless you are familiar with jquery ajax, this code would not be much of help though, but if you decide to use jquery ajax, then you can use this code:
$(document).ready(function(){
$.ajax({
url:"getdeals.php?sid="+sid+"&limit="+limit+"&cat="+cat,
type:"GET",
dataType:'json',
success:function(data){
//data contains value like {countDownId:'someid',countDownDate:'somedate'},
//its in json format,
// in your getdeals.php write some thing like this
// die (json_encode(array('countDownId'=>'someid','countDownDate'=>'somedate')));
$("#countdown_"+data.countDownId).countdown({
date: data.countDownDate,
format: "on"
},
function() {
// callback function
});
}
});
});
Upvotes: 1