简体   繁体   中英

Form - submit POST request to multiple iframes

I know that it is possible to submit POST request to an iframe by using target attribute.

Is there any way to submit POST request from one form to multiple iframes at the same time?

Both HTML and JS solutions are acceptable.

A Javascript solution would be your best bet.

<form id="form">
  <input type="text" name="xyz" value="random data" />
  <button type="submit">Go</button>
</form>

In JS, you would attach an event listener to your form when it is submitted. The listener would trigger a function that could send the form data to multiple targets:

var form = document.getElementById("form");

if(form.attachEvent) {
  form.attachEvent("submit", submitForm);

} else {
  form.addEventListener("submit", submitForm);
}

function submitForm(e) {
  if(e.preventDefault) {
    e.preventDefault();
  }

  this.target = "first_iframe";
  this.submit();

  var secondForm = this.cloneNode();
  secondForm.target = "second_iframe";
  secondForm.submit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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