0

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.

Community
  • 1
  • 1
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
  • Just curious but why would you want to do that? – Nick Zuber Dec 15 '15 at 15:12
  • @NickZ good question! I have markup with two iframes, one is shown for devices with large viewports > 1000px and other is shown for mobile devices. Both iframes should have the same source but different styling and positioning. – Limon Monte Dec 15 '15 at 15:14
  • So would you rather want to submit the form to one of these iframes? Whichever one is in use? Or am I misunderstanding something – Nick Zuber Dec 15 '15 at 15:17
  • @NickZ I'm using the [Bootstrap](http://getbootstrap.com/) and my project is responsive. By resizing browser you can see one or another version, so both iframes should contain actual response. – Limon Monte Dec 15 '15 at 15:20

1 Answers1

1

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();
}
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Nicholas F
  • 141
  • 4