Solved

.submit()

Posted on 2002-07-09
9
938 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

752 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