Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I submit a form using JavaScript?

Tags:

javascript

I have a form with id theForm which has the following div with a submit button inside:

<div id="placeOrder"      style="text-align: right; width: 100%; background-color: white;">     <button type="submit"             class='input_submit'             style="margin-right: 15px;"             onClick="placeOrder()">Place Order     </button> </div> 

When clicked, the function placeOrder() is called. The function changes the innerHTML of the above div to be "processing ..." (so the submit button is now gone).

The above code works, but now the problem is that I can't get the form to submit! I've tried putting this in the placeOrder() function:

document.theForm.submit(); 

But that doesn't work.

How can I get the form to submit?

like image 218
Nate Avatar asked Mar 24 '12 21:03

Nate


People also ask

Which method is used to submit forms in JavaScript?

The formmethod attribute defines the HTTP method for sending form-data to the action URL. The formmethod attribute overrides the method attribute of the <form> element. The formmethod attribute is only used for buttons with type="submit".

What does submit () do in JavaScript?

The method form. submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.

Can JavaScript handle forms?

With JavaScript at your side, you can process simple forms without invoking the server. And when submitting the form to a CGI program is necessary, you can have JavaScript take care of all the preliminary requirements, such as validating input to ensure that the user has dotted every i.


1 Answers

Set the name attribute of your form to "theForm" and your code will work.

like image 107
Andrew Hare Avatar answered Sep 18 '22 20:09

Andrew Hare