0

I have jquery which print the remaining time of quiz to my page. It works fine. Now I want to put condition if else to output of timer_check.php's output. I have put condition if remaining time = 0 then echo timeout else print remaining time. My jquery is in quiz.php page. I want to check if output come from timer_check.php is timeout then All answer should submit to database else print remaining time in tableHolder div. So my question is how to put if else condition for output come from timer_check.php to quiz.php to check submit the quiz or print remaining time?

$(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"));
}
2
  • show your timer_check.php Commented Feb 20, 2015 at 5:40
  • @Outlooker above is code of timer_check.php Commented Feb 20, 2015 at 5:46

1 Answer 1

2

Rather than using the load function you could try using an ajax function.Hope this might give you some idea mate.. :)

    $(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()

Sign up to request clarification or add additional context in comments.

6 Comments

what result return? it is not showing remaining time to my page. And I have not any idea about above code.
result variable returns what is been echoed from your php file.Try putting an alert(result) before the if statement
Your condition is wrong if result == "time out" then submit. And other query is every second my jquery call timer_check.php how to implement same?
I want to call every second this function how to implement this?
try adding setTimeout(refreshTable, 1000); after $('#tableHolder').html(result);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.