如何将数据从MySQL转换为jQuery变量?

时间:2012-04-13 04:13:52

标签: jquery mysql ajax

jQuery的新功能。这就是我到目前为止 - 基本上,我希望jQuery + ajax获取某个项目的最新日期。该脚本每1秒刷新一次。

这是剧本:

<script>
var auto_refresh = setInterval(
function()
{
$.ajax({
    type: "POST",
    url: "gettime.php",
    data: ({i : <?=$itemid;?>}),
    function(data){
        var s = data;
    }
});
var t = s.split(/[- :]/); //split time from mysql
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
$('#defaultCountdown').countdown({until: d,compact: true, 
description: ''});
}, 1000);
</script>

这是gettime.php

<?php 
include("inc/dblink.inc");
$itemid = intval($_POST['i']);
$row = mysql_fetch_array(mysql_query("SELECT * FROM test WHERE itemid='$itemid' ORDER BY bidid DESC LIMIT 0,1"));
$date = $row['date'];
echo $date; //sample date is 2012-04-13 10:03:37
?>

1 个答案:

答案 0 :(得分:3)

你必须在ajax回调中做所有事情,因为datas仅在那里可用(运行时):

var auto_refresh = setInterval(function(){
    $.ajax({
        type: "POST",
        url: "gettime.php",
        data: ({i : <?=$itemid;?>}),
        complete: function(data){
            var s = data;
            var t = s.split(/[- :]/); //split time from mysql
            var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
            $('#defaultCountdown').countdown({until: d,compact: true, description: ''});
        }
    }); 
}, 1000);