Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 920
Alex Churchill Avatar asked Jul 29 '11 19:07

Alex Churchill


People also ask

How do I automatically submit a form without clicking?

Submit a Form Using JavaScript The most simple way to submit a form without the submit button is to trigger the submit event of a form using JavaScript. In the below example we are going to create a function to submit a form. We will set that function at onclick event of a div tag.

What is submit form?

The form submit() Method in HTML DOM is used to send the form data to the web-server. It works as same as submit button.

Does submit need to be inside form?

You can tie a submit button to a form that the button doesn't live inside of. The trick is to give the form an id and then reference that id with the button's form property. With this setup, clicking the Submit button will cause the form to be submitted. See the MDN Button docs for more details.

What happens when you click submit on a form?

The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.


2 Answers

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>
like image 70
fatnjazzy Avatar answered Oct 01 '22 19:10

fatnjazzy


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

like image 39
Barry Kaye Avatar answered Oct 01 '22 19:10

Barry Kaye