How to Submit a Form Using JavaScript

Generally, a form is submitted when the user presses a submit button. However, sometimes, you may need to submit the form programmatically using JavaScript.

JavaScript provides the form object that contains the submit() method. Use the ‘id’ of the form to get the form object.

For example, if the name of your form is ‘myform’, the JavaScript code for the submit call is:


But, how to identify a form? Give an id attribute in the form tag

<form id='myform' action=''>

Here is the code to submit a form when a hyperlink is clicked:

<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
<a href="javascript: submitform()">Search</a>
<script type="text/javascript">
function submitform()

Click the link below to see the code in action:
JavaScript Form Submit Example 1

Make forms with custom Submit Button

Simfatic Forms : customizable formsUse Simfatic Forms to visually design your HTML Forms.
Simfatic Forms

Conducting Form Validations

You can make your form validations very easy by using the Form Validator Script. (See JavaScript Form Validation : quick and easy! ).

The form validation script uses the onsubmit() event of the form to validate the input. The browser does not trigger the onsubmit event if you call the submit method programmatically. Therefore, if the form is using the form validator script, call the onsubmit method also to trigger the validation.

See the example below:

<!-- Include the validator script-->
<script src="/scripts/gen_validatorv2.js"

<form id="myform" action="handle-data.php">
Search: <input type='text' name='query' />
<A href="javascript: submitform()">Search</A>

<!-- Add validations to the form-->
<script type="text/javascript">
var myformValidator = new Validator("myform");
        "Please enter the value for query");

<!-- The function that submits the form-->
<script type="text/javascript">
function submitform()
 {//this check triggers the validations

See the code in action!

Using image for submitting the form

Instead of the default gray submit button, you may like to use an image. There are two ways to use an image instead of the button.

Method 1 : Use the ‘Image’ input type

Standard HTML provides an input type ‘image’. You can use image input type to create a submit button.
See the code below:

<form name="myform" action="">
Search: <input type='text' name='query' />
<input type="image" src="go.gif" />

JavaScript form submit example 3

Method 2 : Use JavaScript Form Submit

Just like in the example above, use JavaScript form submit() function to submit the form. The sample code below shows the same:

<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
<a href="javascript: submitform()">
<img src="go.gif" width="33" height="19" border="0" />
<script type="text/javascript">
function submitform()
    if(document.myform.onsubmit &&

Image input form submit example

Want to have multiple submit buttons in a form?

Quickly create feature-rich web forms

Simfatic Forms – HTML Form maker
Using Simfatic Forms you can create feature-rich web forms.
The features include highly customizable submit/reset buttons. Download here.

Comments on this entry are closed.

  • hi
    I have to submit form data and view also from db

    and i am using rest api,and now i am using stringify function in js for access object how to do please please help me

  • Thanks ! And what is the code on “submit-form.php” ? (i.e. how to actually collect the data ?

  • Thanks a lot for the explanation.

  • Try my form library –
    It is good about ajax sumbit, validation and other

  • Try my form library
    In it you can easily do sumbitting to server.

  • Hi – i spent 3 hours yesterday trying to get the image to auto upload AND display in the preview box. I dont want to have to click submit.. but didnt get anywhere. I tried adding in the ajax code into the onchange event, but not working..

    can u post some js to make this auto preview/upload when u have selected a file?

    thanks! :p

  • Is it possible to post with action=”external urls” ? Thanks.

  • I don’t wanna use link and submit button.
    There are three field in the form and after filling the form field it will be submitted automatically by using any action on form tag not in any field…….

  • Genial, me sirvió mucho, solucione el problema

  • this is a great solution.

  • Historically, I have used the SUBMIT action=”filename.php” field to invoke a CAPTCHA pass-or-fail event, and respond to the form user.
    Of course, in my case (reference example below), javascript form validation overrides the action tag on form submission.
    My question is: Is the use of CAPTCHA even necessary if I’m using form validation? and, if so, what is the easiest way to go about implementing CAPTCHA and form validation?
    //* My form submission example – passing the input variable “captcha_input” on form submission


  • En cualquier caso deberan cambiar document.myform.submit(); por document.getElementById(“myform”).submit(); para no tener problemas con Firefox o Chrom…



  • Just thought I would let you know that you may want to change this sentence:

    For example, if the name of your form is ‘myform’, the JavaScript code for the submit call is:

    to say “if the id” of you form…

    There is both a name and id attribute for most elements, and this may confuse some newcomers, apart from that, though, I know it should be obvious to almost everyone, since you gave an example. Thanks!

  • I seriously love your site.. Very nice colors & theme.
    Did you build this web site yourself? Please
    reply back as I’m wanting to create my own blog and want to know where you got this from or just what the theme is called. Thanks!

  • Thx a lot guys…
    This will help me much…

  • Is there any way to control the highlighted length of the field in question that you mention in your Showing the error messages next to the input element section of your JavaScript Form Validation Script: More features page? I like this feature to highlight the error in this way, but do not like that it goes all the way across the screen instead of just highlighting the failed validation field in question only.

    Thanks for sharing this helped me a lot for a project :o)

    • make use of table and you can specifically give size for that Td tag i did the same way..

  • How to make the page not to refresh after submit? Thanks!

    • You can use ajax for this.