Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making A Submit Button Not Submit 2

Status
Not open for further replies.

iaresean

Programmer
Joined
Mar 24, 2003
Messages
570
Location
ZA
Hello ALL!

I have a submit button with a function onClick="checkStats($info);" which causes the javascript checkStats() function to be called and processed before the page continues the submit. I want to know if it possible to stop the submit. I am passing data to the checkStats() function and if a certain criteria is met I want to stop the submission from taking place and instead make one of my hidden tables visible.

Here is small form example:
Code:
<form name="testform" method="post" action="test.cgi">
   <input type="text" name="yourname">
   <input type="submit" name="SubmitProcess" value="Click Me" onClick="checkStats($info)">
</form>

I hope that I have made my intentions clear.

I appreciate any help in any form, be it, tutorials, links, advice...etc.

Thanks in advance.

Sean. [peace]
 
An immediate fix is to change the type to button (not submit) and to use document.formname.submit() in the success clause of your function.

Here is a sample based on your code that solves the problem properly though...

Code:
<html>
<head>
<script type="text/javascript">
function testMe()
{
  if (document.getElementById("yourname").value == "wibble") return false;
  return true;
}
</script>
</head>
<body>
<form name="testform" method="post" action="test.cgi">
  <input type="text" name="yourname"> (try typing <b>wibble</b>)<br>
  <input type="submit" name="SubmitProcess" value="Click Me" onClick="return(testMe())">
</form>
</body>
</html>

Hope that gets you going again,
Jeff
 
Returning true or false in the onclick event will define whether or not the submit happens. If you design your checkStats function to return a true or false, you could do something like
Code:
<form name="testform" method="post" action="test.cgi">
   <input type="text" name="yourname">
   <input type="submit" name="SubmitProcess" value="Click Me" onClick="if checkStats($info) {return true} else {show hidden div;return false}">
</form>
Greg.
 
Both are valuable piece's of information guys!

I think I will go for grega's solution though because I am probably going to have more than one submit button in my form.

Thanks guys!

Sean. [peace]
 
Actually, you guys posted the same idea in your suggestions, must have sent them almost at the same time.

You both get a star. ;-)

Sean. [peace]
 
As far as I know, a form can only have one submit button. You can, however, make that button do different things by using javascript to change the forms action.

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top