无法使用JS表单提交功能发布

时间:2018-06-27 03:23:42

标签: javascript php post html-form html-input

在此网页中,我有一个带有提交按钮的可见表单,称为表单A。它具有发布操作。

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

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

但是,如果我添加了真正的按钮,它可以工作并发布到适当的位置。

 <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>

1 个答案:

答案 0 :(得分:0)

您应使用DOMContentLoaded而不是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>