Alex Churchill
Alex Churchill

Reputation: 4957

Submit a form in the background

How can I submit a form to a third-party website without it causing the page to reload (i.e. in the background)?

For some background, I'm trying to programmatically log a user into their google account in the background.

I can programmatically log them in using the following:

var div = document.createElement("div");
div.innerHTML = FORM_HTML;
var form = div.getElementsByTagName("form")[0];
form.action = "https://www.google.com/accounts/ServiceLoginAuth";
form.GALX.value = FORM_GALX; // dynamically obtained using AJAX
form.Email.value = USER_EMAIL;
form.Passwd.value = USER_PASSWORD;
form.submit();

This logs me in and opens up my Google dashboard. How can I prevent form.action from redirecting me?

Please note that this div is never actually added to a document, which is preferable but not required.

Upvotes: 12

Views: 16455

Answers (3)

Nayan Bagia
Nayan Bagia

Reputation: 89

you can use ajax or jquery for request to submit form in background if you want to submit form to secure connection i think they want allow submit form in this way..

Upvotes: 0

Barry Kaye
Barry Kaye

Reputation: 7759

You need to use AJAX to generate XMLHttpRequest best using any popular JS library such as jQuery, Prototype JS or Mootools.

Upvotes: 1

fatnjazzy
fatnjazzy

Reputation: 6152

you can set the target to submit the form to an IFRAME
like that

<form target="transFrame" method="POST" action="http://...."  ></form>
<iframe style="" name="transFrame" id="transFrame"></iframe>

Upvotes: 21

Related Questions