An HTML form after filled with information is sent to a server awaiting an action to trigger (as defined by the action atribute). It can be done with the submit() method or the onsubmit() event.

The submit() method

The submit() method is equal to the submit button which is clicked upon filling the form. It takes the form parameters and sends it to file given in action attribute. A drawback of submit() method is, if server is not responding or is slow, an user can trigger submit() multiple times for same form values leading to server processing duplicating requests. It leads to erroneous  results in banking or marketing websites. In such cases disabling submit button or using onsubmit() event to send request is effective.

Syntax for submit() method

formName.submit();

The action property (same as the action attribute) determines where to submit the form once it's been filled:

The action property

The action property is supported in all major browsers. It’s syntax is as below:

Syntax for setting action property

formName.action = URL;

where:

  • URL specifies the destination to send form data. It can be relative URL like a file in the web page folder or an absolute URL.

Syntax for returning action property

formName.action;

The method property

The method property is an important property while submitting forms. It determines how the form data is sent to the page in the server (specified by action property). The method can have ‘get’ or ‘post’ values.

 Syntax for setting method property

formName.method = “get”;

or

formName.method = “post”;

Syntax for returning method property

formName.method;

In the simple example below, the form accepts the input parameters and on submit it sendsthem to form_action.php script file for further action. I nreality the form_action.php file call is here only for the example purpose and actually doesn't exist. Since the method is set to 'get' the input parameters are sent within the URL and may look something like this:

http://www.brenkoweb.com/JS/form-scripting/form_action.php?name=rani&city=delhi

Simple example of submit method in JavaScript

 

›› go to examples ››