自动php表单使用javascript提交

时间:2015-10-06 09:25:33

标签: javascript php html forms

我正在处理一个PHP表单,该表单应在计时器到期后自动提交。当提交表单时,我希望将所有数据从另一个php页面获取。我在做什么

 if (isset($_GET['id']))

{
$query="SELECT * from Question_bank";
$result= mysqli_query($connection,$query);
confirm_query($result);
?>
        <form action="answer.php" name="form1" method="post" id="quiz">
        <?php
while ($read_all_data = mysqli_fetch_assoc($result))
{ 
    $_SESSION['id']=1;

$id=$read_all_data['question_id'];
$a=$read_all_data['a'];
$b=$read_all_data['b'];
$c=$read_all_data['c'];
$d=$read_all_data['d'];
 echo $read_all_data['question']."</br>";
 echo "A:<input type ='radio' value ='a'  name='$id' >".$read_all_data['a']."</br>";
 echo "B:<input type ='radio' value ='b'  name='$id' >".$read_all_data['b']."</br>";
 echo "C:<input type ='radio' value ='c'  name='$id' >".$read_all_data['c']."</br>";
 echo "D:<input type ='radio' value ='d'  name='$id' >".$read_all_data['d']."</br>";

}
}
?>
<input type="submit"  value="Submit_Quiz"  /> // it works fine in this way and auto submitted after timer reaches to zero

</form>

但我也想要提交按钮中的name属性来获取从表单到下一页的所有值。但是当我把“name = submit”时,它不会自动提交

<input type="submit"  value="Submit_Quiz" name="submit"  /> 

javascript代码

<script type="text/javascript"> 

var startCountDown = document.getElementById('startCountDown');
startCountDown.addEventListener('click',function(){
    var startTime = document.getElementById('startTime').value;
    var startDate = document.getElementById('startDate').value;
    var duration = document.getElementById('duration').value;

    function checkDateTime(){
        setTimeout(function(){
            if(new Date(startDate + " "+ startTime) <= new Date()){
                display_c(duration);
            }else{
                checkDateTime();
            }
            document.getElementById('minutesLeft').innerHTML = Math.round((new Date(startDate + " "+ startTime) - new Date() )/ (1000*60));
            console.log('Minutes left ',(new Date(startDate + " "+ startTime) - new Date() )/ (1000*60));
        },1000);
    }
    checkDateTime();
   console.log('button clicked',startTime,startDate,duration); 
});

function display_c(start)
{
window.start = parseFloat(start);
var end = 0 // change this to stop the counter at a higher value
var refresh=1000; // Refresh rate in milli seconds
if(window.start >= end ){
mytime=setTimeout('display_ct()',refresh)
}
else 

    callback();
}

 function callback(){
    window.setTimeout(function() {
    document.forms['form1'].submit();
}, 500);
      }
/*double seconds = milliseconds / 1000.0;*/

function display_ct() {
// Calculate the number of days left
var days=Math.floor(window.start / 86400); 
// After deducting the days calculate the number of hours left
var hours = Math.floor((window.start - (days * 86400 ))/3600)
// After days and hours , how many minutes are left 
var minutes = Math.floor((window.start - (days * 86400 ) - (hours *3600 ))/60)
// Finally how many seconds left after removing days, hours and minutes. 
var secs = Math.floor((window.start - (days * 86400 ) - (hours *3600 ) - (minutes*60)))

var x = window.start + "(" + hours + " Hours " + minutes + " Minutes and " + secs + " Secondes " + ")";


document.getElementById('ct').innerHTML = x;
window.start= window.start- 1;

tt=display_c(window.start);
}

</script>

请帮帮我

2 个答案:

答案 0 :(得分:0)

而不是mytime = setTimeout('display_ct()',refresh)

写mytime = setTimeout('display_ct',refresh)

可能会有所帮助

答案 1 :(得分:0)

使用如下。

window.onload=function(){ 
    window.setTimeout(document.quiz.submit.bind(document.quiz), 5000);
};

OR

window.onload=function(){ 
    window.setTimeout(function() { document.quiz.submit(); }, 5000);
};

有关详情,请查看Auto submit form after 5 seconds