Solved

.submit()

Posted on 2002-07-09
9
923 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

803 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