检查jquery中是否有其他条件

时间:2015-02-20 05:21:07

标签: javascript php jquery ajax if-statement

我有jquery将测验的剩余时间打印到我的页面。它工作正常。现在我想把条件if else输出到timer_check.php的输出。如果剩余时间= 0则放置条件,然后回显超时,否则打印剩余时间。我的jquery在quiz.php页面中。我想检查输出是否来自timer_check.php是超时然后所有答案应提交给数据库否则打印tableHolder div中的剩余时间。所以我的问题是如何把if else条件输出来自timer_check.php到quiz.php以检查提交测验或打印剩余时间?

$(document).ready(function() {
  refreshTable();
});

function refreshTable(){
    $('#tableHolder').load('timer_check.php', function(){
       setTimeout(refreshTable, 1000);
    });
}

timer_check.php

session_start();
include("database.php");
$sql = "select * from ctip_test where test_id = '$_SESSION[testid]'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $starttime = $row['attemp_time'];
}
$datetime1 = new DateTime($starttime);
$datetime1->add(new DateInterval('P0Y0M0DT2H0M0S'));
$currenttime =date('Y-m-d H:i:s');
$datetime2 = new DateTime($currenttime);
$diff = $datetime2->diff($datetime1);
$check_timeout = (array) $datetime2->diff($datetime1);
$hour = $check_timeout['h'];
$minute = $check_timeout['i'];
$second = $check_timeout['s'];
if($hour=="0" && $minute =="0" && $second =="0"){
    echo "time out";
}
else{
  echo($diff->format("%h hours %i minutes and %s seconds are remaining\n"));
}

1 个答案:

答案 0 :(得分:2)

而不是使用加载函数,你可以尝试使用ajax函数。希望这可能会给你一些想法配对.. :)

    $(document).ready(function() {
        refreshTable();
    });

    function refreshTable(){
        $.ajax({
                    url: "timer_check.php",
                    success: function (result) {
                        if(result == "time out"){
                            $("#myId").submit(); // myId is the id of the form
                        }else{
                            $('#tableHolder').html(result);
                           setTimeout(refreshTable, 1000);
                        }
                    }
                });
    }

<强> FYI

ajax()