0

刚从worldofwebcraft.com得到了我项目考试系统的一些代码。我只知道PHP,实际上我对javascripts一无所知,因为我们的老师还没有教过我们。请帮助我了解如何在用户重新加载页面或打开新标签时自动提交表单。

继承人的代码:

<?php if(isset($_GET['question'])){
    $question = preg_replace('/[^0-9]/', "", $_GET['question']);
    $next = $question + 1;
    $prev = $question - 1; ?>

<script type="text/javascript">
function countDown(secs,elem) {
    var element = document.getElementById(elem);
    element.innerHTML = "You have "+secs+" seconds remaining.";
    if(secs < 1) {
        var xhr = new XMLHttpRequest();
        var url = "userAnswers.php";
            var vars = "radio=0"+"&qid="+<?php echo $question; ?>;
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            alert("You did not answer the question in the allotted time. It will be marked as incorrect.");
            clearTimeout(timer);
    }
}
xhr.send(vars);
        document.getElementById('counter_status').innerHTML = "";
        document.getElementById('btnSpan').innerHTML = '<h2>Times Up!</h2>';
        document.getElementById('btnSpan').innerHTML += '<a href="exam.php?question=<?php echo $next; ?>">Click here now</a>';

    }
    secs--;
    var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
}
</script>
<script>
function getQuestion(){
    var hr = new XMLHttpRequest();
        hr.onreadystatechange = function(){
        if (hr.readyState==4 && hr.status==200){
            var response = hr.responseText.split("|");
            if(response[0] == "finished"){
                document.getElementById('status').innerHTML = response[1];
            }
            var nums = hr.responseText.split(",");
            document.getElementById('question').innerHTML = nums[0];
            document.getElementById('answers').innerHTML = nums[1];
            document.getElementById('answers').innerHTML += nums[2];
        }
    }
hr.open("GET", "questions.php?question=" + <?php echo $question; ?>, true);
  hr.send();
}
function x() {
        var rads = document.getElementsByName("rads");
        for ( var i = 0; i < rads.length; i++ ) {
        if ( rads[i].checked ){
        var val = rads[i].value;
        return val;
        }
    }
}
function post_answer(){
    var p = new XMLHttpRequest();
            var id = document.getElementById('qid').value;
            var url = "userAnswers.php";
            var vars = "qid="+id+"&radio="+x();
            p.open("POST", url, true);
            p.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            p.onreadystatechange = function() {
        if(p.readyState == 4 && p.status == 200) {
            document.getElementById("status").innerHTML = '';
            alert("Your answer was submitted.");
            var url = 'exam.php?question=<?php echo $next; ?>';
            window.location = url;
    }
}
p.send(vars);
document.getElementById("status").innerHTML = "processing...";

}
</script>
<script>
window.oncontextmenu = function(){
    return false;
}
    document.onkeydown = function() {
        if(event.keyCode == 116) {
                event.returnValue = false;
                event.keyCode = 0;
                return false;
               }
    }


</script>
4

1 回答 1

0

我会做类似的事情:

window.onbeforeunload = function() {
    post_answer(); 
}

如果post_answer() 您的“提交表单”处理程序。从代码中无法准确判断。

于 2013-09-12T16:24:35.210 回答