douyou7797 2013-08-20 00:58
浏览 24

倒计时结束后提交表格

I'm trying to submit the document (return_url) only after the countdown finishes. In my current code below the document is submitted just after the countdown starts. How can I make the countdown finish before submitting the document_url?

Code:

<body>
    <center>
        <form name="redirect"><font face="Helvetica"><b>
            Thank you! You will be redirected in<input type="text" size="1" style="font-size:25px" name="redirect2">seconds.</b></font>
        </form></form>
    </center>

    <form name="return_url" method="post" action="confirm.php">
        <input type="hidden" name="order" value="1" />
        <input type="hidden" name="price" value="100" />
    </form>
</body>
</html>

<script language="javascript" type="text/javascript">
    var targetURL="<?php print_r($_POST['url_retorno']);?>"
    var countdownfrom=10
    var currentsecond=document.redirect.redirect2.value=countdownfrom+1

    function countredirect(){
        if (currentsecond!=1){
            currentsecond-=1
            document.redirect.redirect2.value=currentsecond
        } else {
            window.location=targetURL
            return
        }
        setTimeout("countredirect()",1000)
    }

    countredirect()

    document.return_url.submit()    
</script>
  • 写回答

2条回答 默认 最新

  • dongtan7998 2013-08-20 01:05
    关注

    Big point, you shouldn't automatically submit forms for users, it's kind of a bad UX design. Instead, you could do the wait before submiting thing which disables the button until the timer is up. And for that, you just need to make use of javascript timers.

    var timer = window.setInterval(updateClock,1000);
    var countdown = 10;
    function updateClock() {
       countdown--;
       if(countdown<=0) {
           window.clearInterval(timer);
           //ALLOW THE FORM TO SUBMIT
           document.getElementById("elementID").disabled = false
       } else {
           document.getElementById("elementID").value = "PLEASE WAIT "+countdown+" SECONDS";
       }
    }
    

    I'm also adding in a side point that you should never trust what people put into forms. Lookup XSS or cross site scripting, and SQL injection to find out why. So you need to use validation (even for numbers). If you want on page validation (which still isn't really secure since it can be by-passed) you could add a onSubmit="validate()" attribute to the form tag to call the validate() function when the user does finally submit it. Using this function also gives you the power to take over the form if needed, since you can execute any javascript you want there and simply returning a "false" value will cause the form to NOT submit.

    ALWAYS VALIDATE YOUR FORM ENTRIES ON THE SERVER LEVEL (ie. in the confirm.php file)

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集