How to Submit a Form Using JavaScript

In normal cases, 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 name 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:
document.myform.submit();

But, how to name a form? Simple! Mention the name attribute in the form tag
<form name='myform' action='formmail.pl'>

Suppose you want to submit a form when the user clicks on a hyperlink. The code below shows how to do it.

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

<SCRIPT language="JavaScript">
function submitform()
{
  document.myform.submit();
}
</SCRIPT>

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

Conducting Form Validations

You can make your form validations very easy by using the Form Validator Script. (See JavaScript Form Validations Made 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"  language="JavaScript"></SCRIPT>

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

<!-- Add validations to the form-->
<SCRIPT LANGUAGE="JavaScript">
var myformValidator = new Validator("myform");
myformValidator.addValidation("query","req","Please enter the value for query");
</SCRIPT>

<!-- The function that submits the form-->
<SCRIPT language="JavaScript">
function submitform()
{
 if(document.myform.onsubmit())
 {//this check triggers the validations
    document.myform.submit();
 }
}
</SCRIPT>

See the code in action!
JavaScript Form Submit Example 2 (with validations)

Using image for submitting the form

Instead of the 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 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="handle-data.pl">
Search: <input type='text' name='query'>
<input type="image" src="go.gif">
</form>

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"
></A>
</form>

<SCRIPT language="JavaScript1.2">
function submitform()
{
 if(document.myform.onsubmit())
 {
 document.myform.submit();
 }
}
</SCRIPT>

Image input form submit example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值