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
Solved

.submit()

Posted on 2002-07-09
9
930 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

829 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