简体   繁体   English

表单 onsubmit 返回 false 并仍然继续提交表单

[英]Form onsubmit returns false and still continues submitting the form

I am using php5.6 and printing html and trying to get a confirm propmt to work with JS.我正在使用 php5.6 并打印 html 并试图获得与 JS 一起使用的确认提示。 Ordering products from weppage it should ask if person is certain that the order is made and he/she is proceeding to checkout after "ok".从 weppage 订购产品时,它应该询问人们是否确定订单已完成,并且他/她在“ok”后继续结帐。 Cancelled to continue shopping or change personal info.取消继续购物或更改个人信息。

Prompt works, if(!conf) is executed and page is redirected to top, but it still submits the form and creates an order on cancel.提示有效,执行if(!conf)并将页面重定向到顶部,但它仍然提交表单并在取消时创建订单。 Here is the code and that is included to other file if it matters.这是代码,如果重要,它会包含在其他文件中。 was searching stackoverflow for a while and didn't see this kind of a problem.正在搜索stackoverflow一段时间,并没有看到这种问题。 Tried to put 'onclick' to button but it was even worse and got some errors.试图将“onclick”放在按钮上,但情况更糟,并且出现了一些错误。

ob_start();
    ?>

    <script type="text/javascript">
        function continueToPayment() {
            var conf = confirm("<?php echo SQL::Translate('payment_confirm_text') ?>");
            if(!conf) {
                window.location.href = '#';
                return false;
            }
            else{
                return true;
            }
        }
    </script>

    <?php
    print "<form onsubmit='return continueToPayment()' action='" . SQL::Table("Pages")->Href("thank_you") . "?pm=cash&ord=$next_reference_number' method=\"POST\">";
    $amount = number_format((($_SESSION['sc_data']['final_total'])/100),2,".","");
    print "<input type='hidden' name='action' value='send_cash_order'>";

    echo ReturnOrderSubmitInfoHtml();

    print "<button type='submit' id='send_order' class='pc-btn pc-btn-sc'>" . SQL::Translate(PrintPaymentText('cash')) . "</button>";
    print "<br/>";

    print "</form>";

    $form = ob_get_clean();

Thank you all for contributng to my problem, I have solved it.感谢大家为我的问题做出贡献,我已经解决了。 so the problem was that there already was a JS file doing check for term accepting and I tried to put js to php which was apparently overrided by that js file.所以问题是已经有一个 JS 文件在检查是否接受术语,我试图将 js 放到 php 中,这显然被那个 js 文件覆盖了。 I put我放

     var conf = confirm("example");
         if(!conf) {
             window.scrollTo(0, 0);
             return false;
        }

there and it worked.在那里,它奏效了。 There was already an event.preventDefault() .已经有一个event.preventDefault() window.scrollTo(0, 0) worked as the same as window.location.href = '#'. window.scrollTo(0, 0) 与 window.location.href = '#' 相同。 Anyway now I need to worry only about translations and make changes to other files than paying with cash.无论如何,现在我只需要担心翻译和更改其他文件,而不是用现金支付。

In the form part it should be onsubmit='continueToPayment(event)' .在表单部分它应该是onsubmit='continueToPayment(event)'

Then in the function you need to stop default form behaviour by using event.preventDefault() method.然后在 function 中,您需要使用event.preventDefault()方法停止默认表单行为。

function continueToPayment (event) {
  var conf = confirm("<?php echo SQL::Translate('payment_confirm_text') ?>")
  if (!conf) {
     window.scrollTo(0, 0)
     event.preventDefault()
     return false
  }
  // else case unnecessary
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM