0

I have three forms, not always all three will be defined (in many cases it's just two), but in any scenarios I would like to send them. Action page will be execute something. Current script works fine with 1 or 2, but not 3 forms.

Any chance that it can be done with simple JS or CF only? Or may be there is better way to handle this?

<form id="CreateTask1" action=action.cfm>
<form id="CreateTask2" action=action.cfm>
<form id="CreateTask3" action=action.cfm>

   <script type="text/javascript">
      function submitforms() {
        if (document.getElementById('CreateTask1') != null)
            document.forms.CreateTask1.submit();
            window.setTimeout(function() {
        if (document.getElementById('CreateTask2') != null)
            document.forms.CreateTask2.submit();
            }, 100);
            window.setTimeout(function() {
        if (document.getElementById('CreateTask3') != null)
            document.forms.CreateTask3.submit();
            }, 100);
        }
    </script>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
SlavaCa
  • 59
  • 1
  • 6

2 Answers2

3

Maybe you should use AJAX to achieve this.

So if you have three different forms to send, there are several ways about making asynchronous requests. We could use some lib like JQuery or even pure javascript with its new features (API Fetch) in a modern way, or using the usual XMLHttpRequest.

Here is the explanation about making multiple XMLHttpRequests in a for loop, which in your case, can be matched with document.forms length.

Something like this:

 function sendForms() {
  var xhr = [],
    i;

  for (i = 0; i < document.forms.length; i++) {
    //for loop
    (function (i) {
      var form = document.forms[i];
      var input = document.getElementById("input1");
      var inputData = encodeURIComponent(input.value);
      xhr[i] = new XMLHttpRequest();
      url = "your_page.php?" + input.name + "=" + inputData;
      xhr[i].open("POST", url, true);
      xhr[i].onreadystatechange = function () {
        if (xhr[i].readyState === 4 && xhr[i].status === 200) {
          console.log(
            "Response from form " + i + " is [ " + xhr[i].responseText + "]"
          );
        }
      };
      xhr[i].send();
    })(i);
  }

}

sendForms();

You may want to construct the parameters to send dynamically, for this you can use FormData to help, or maybe consider use JQuery to ease up the steps.

Sorry to not provide a more detailed answer earlier, but I don't know if you are able to use AJAX in your project, but for me, using these timeouts don't look like a good solution.

1

I suppose you could

 <form id="CreateTask1" action="action.cfm" target="iframe1"></form>
 <iframe name="iframe1" src="blank.html"></iframe>

 <form id="CreateTask2" action="action.cfm" target="iframe2"></form>
 <iframe name="iframe2" src="blank.html"></iframe>

 <form id="CreateTask3" action="action.cfm" target="iframe3"></form>
 <iframe name="iframe3" src="blank.html"></iframe>

<script type="text/javascript">
  function submitforms() {
    if (document.getElementById('CreateTask1') != null)
        document.forms.CreateTask1.submit();
        window.setTimeout(function() {
    if (document.getElementById('CreateTask2') != null)
        document.forms.CreateTask2.submit();
        }, 100);
        window.setTimeout(function() {
    if (document.getElementById('CreateTask3') != null)
        document.forms.CreateTask3.submit();
        }, 100);
    }
</script>
James A Mohler
  • 11,060
  • 15
  • 46
  • 72