Solved

.submit()

Posted on 2002-07-09
9
912 Views
Last Modified: 2009-12-16
I have a form with validation code and want to submit the form via an image as a button.

The form setup is:
<form name=myForm action=myAction.asp method=post onSubmit="return doValidate(myForm)">

The submit image is:
<a href=# onClick=submit()><image src=myImage.gif></a>

The javascript validation is:
function doValidate(formObj){
return true;
}

This code never fires doValidate.

How is the submit() method used?
0
Comment
Question by:vogtster
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 7141409
<form name=myForm action=myAction.asp method=post onSubmit="return doValidate('myForm')">

or :

<form name=myForm action=myAction.asp method=post onSubmit="return doValidate(this)">

0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 7141410
This:

<a href=# onClick=submit()><image src=myImage.gif></a>


may need to be:

<a href=# onClick="javascript:document.forms[0].submit()"><image src=myImage.gif></a>


Fritz the Blank
0
 

Author Comment

by:vogtster
ID: 7141441
That does not work. I know because I put an "alert('validate')" in the doValidation function to help me see if it is firing and I never see the alert.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 7141481
Let me know how this works for you. If you like, I can generalize it so that you don't have include the name of the form in the function:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function doValidate(formObj){
alert("Hey there!");
document.myForm.submit();
return true;
}
//-->
</SCRIPT>


</HEAD>
<BODY>

<form name="myForm" action="myAction.asp" method=post onSubmit="return doValidate(this)">

The submit image is:
<a href=# onClick="javascript:doValidate()"><img src=myImage.gif></a>



</BODY>
</HTML>
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:knightEknight
ID: 7141511
or this:

<a href="#" onClick="if(doValidate(document.myForm))document.myForm.submit();return false;"><img src="myImage.gif"></a>
0
 

Author Comment

by:vogtster
ID: 7141536
I know I can call the doValidate function directly from the image click.

I don't want to call doValidate from the image click, but rather cause the form's onSubmit function to fire.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7141567
When you call the submit method of a form the onSubmit will NOT be fired. This is the design of the form! There is no way you can call the submit method and have the onSubmit event fired.

You can either use:
<input type="image" src="myImage.gif" border="0">
instead of:
<a href=# onClick=submit()><image src=myImage.gif></a>

or:

<a href=# onClick="if(doValidate(document.forms['myForm'])document.forms['myForm'].submit()"><image src="myImage.gif"></a>


CJ

0
 
LVL 22

Accepted Solution

by:
CJ_S earned 100 total points
ID: 7141572
Sorry, I repeated KeK's comment (didn't see it).

>> I don't want to call doValidate from the image click, but rather cause the form's onSubmit function to fire.

You can't with your current design, use input type=image instead (please note that this tag also needs to be INSIDE the form!)
0
 

Author Comment

by:vogtster
ID: 7141592
Oops! I've been using buttons for so many years I forgot all about the image type input button. Thanks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now