简体   繁体   English

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

[英]It is not able to post with JS form submit function

In this webpage , I have a visible form with a submit button ,called form A.It has a post action. 在此网页中,我有一个带有提交按钮的可见表单,称为表单A。它具有发布操作。

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

I want to do an invisible form that extract some of the data from form A , with the method of hidden input button .It auto-executes the form and post to the another place with the JS. 我想做一个不可见的表单,使用隐藏的输入按钮的方法从表单A提取一些数据。它会自动执行表单并用JS发布到另一个地方。

However, it works and posts to appropriate place if I add the real button . 但是,如果我添加了real button,它将起作用并发布到适当的位置。

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

Here is my code (without the real button): 这是我的代码(没有真正的按钮):

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

You should use DOMContentLoaded instead of load to ensure that the DOM elements are loaded successfully. 您应该使用DOMContentLoaded而不是load来确保DOM元素已成功加载。

Try to do something like below: 尝试执行以下操作:

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

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

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