使用jquery在php中10秒后自动点击提交

时间:2017-11-01 03:56:27

标签: javascript php jquery

我正在尝试使用10sPHP jquery之后自动提交。但没有采取任何行动。

我试过了:

<?php echo "<div class="page-header">
            <h1 class="h2"></h1>
        </div>
        <script>
            var checkState = function(){
      jQuery.ajax({
        url: 'check_diffex.php?od=$scdate'
      }).done(function(data){
        if(data.diffex >= 10) {
        $('#quizsb').submit();          
      });    
    }    
    checkState();    
            </script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jque‌​ry.min.js"></script>
    <form method="post" action="qform.php?srn=<?php echo $srn ?>&id=<?php echo $id ?>" enctype="multipart/form-data" class="form-horizontal" id="quizsb">
.....
</form
"?>

check_diffex.php

<?php

header('Content-Type: application/json');
if(isset($_GET['od'])){
    $deotd = $_GET['od'];
}
date_default_timezone_set('Asia/Calcutta');
$cdate = date('Y-m-d H:i:s ', time());
$scdate = strtotime($cdate);

$e = $scdate - $deotd;

// You would calculate a real value here
echo json_encode([
  'diffex' => $e
]);
?>
  

在回答之前请查看此comment

2 个答案:

答案 0 :(得分:1)

使用setTimeout函数。 下面的代码在页面加载10秒后自动提交表单

&#13;
&#13;
<!DOCTYPE html>
<html>
<body>

<p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p>

<form id="myForm" action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="myFunction()" value="Submit form">
</form>

<script>
    setTimeout(function(){ document.getElementById("myForm").submit(); }, 10000);
    
</script>

</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您不希望用户增加超时,请使用Cookie,因此请将Cookie设置为10秒生命周期并在此之后删除Cookie。请在10秒后应用您的逻辑删除cookie,并始终检查cookie是否已设置,如果不是,则提交表单。

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}