它无法 post 使用 JS 表单提交功能

It is not able to post with JS form submit function

在这个网页中,我有一个带有提交按钮的可见表单,称为表单 A.It 有一个 post 操作。

<form name="payFormCcard" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">

我想做一个隐藏的表单,从表单A中提取一些数据,使用隐藏输入按钮的方法。它自动执行表单并post到另一个地方用JS。

但是,如果我添加真正的按钮,它会工作并且 posts 到适当的位置。

 <input type="submit" name="submission_button" value="Click here if the site is taking too long to redirect!">

这是我的代码(没有真正的按钮):

   <form name="A"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">  <== first visable form 
    .....
    //invisible table
      <form name="payForm" method="post" action=" https://test.paydollar.com/b2cDemo/eng/payment/payForm.jsp">

      <input type="hidden" name="merchantId" value="sth">
    <input type="hidden" name="amount" value="</?php echo $input_amount; ?>" >

    <input type="hidden" name="orderRef" value="<?php  date_default_timezone_set("Asia/Taipei");  $date = date('m/d/Y h:i:s a', time()); echo $date ; ?>">
    <input type="hidden" name="currCode" value="sth" >
    <input type="hidden" name="mpsMode" value="sth" >
    <input type="hidden" name="successUrl" value="http://www.yourdomain.com/Success.html">
    <input type="hidden" name="failUrl" value="http://www.yourdomain.com/Fail.html">
    <input type="hidden" name="cancelUrl" value="http://www.yourdomain.com/Cancel.html">
   ...

    <!--     <input type="submit" name="submission_button" value="Click here if the site is taking too long to redirect!">-->
    </form>

    <script type="text/javascript">
        //Our form submission function.
        function submitForm() {
            document.getElementById('payForm').submit();
        }
        //Call the function submitForm() as soon as the page has loaded.
        window.onload = submitForm;

    </script>

您应该使用 DOMContentLoaded instead of load 来确保 DOM 元素加载成功。

尝试执行如下操作:

<script type="text/javascript">
    //Our form submission function.
    function submitForm() {
        document.getElementById('payForm').submit();
    }
    //Call the function submitForm() as soon as the document has loaded.

    document.addEventListener("DOMContentLoaded", function(event) {
        submitForm();
    });

</script>