evabb
evabb

Reputation: 405

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.

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

However, it works and posts to appropriate place if I add the 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>

Upvotes: 2

Views: 993

Answers (1)

Terry Wei
Terry Wei

Reputation: 1531

You should use DOMContentLoaded instead of load to ensure that the DOM elements are loaded successfully.

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>

Upvotes: 1

Related Questions