Solved

about onSubmit javascript

Posted on 2003-11-28
9
4,754 Views
Last Modified: 2010-05-18
Hi There ,
I m using a jsp page with javascript.
In it I m submitting a form with two submitting buttons

My jsp page is "insert.jsp"

<script language="javascript">
function finalCheck(){
var formcount=0;
var cre = document.insertForm.credit.value;
var deb = document.insertForm.debit.value;
var button1=document.insertForm.insert.value;
var button2=document.insertForm.show.value;
alert("Button1 and Button2 are "+button1+"\n"+button2);

//if(button1 != button2){
      if (cre==0 || deb ==0)
      {
            alert("Sorry !!!\n\nYou can't insert zero values in database");
            return false;
                  }
      else
            {
                        
            if (formcount == 0)
                  {
                  formcount++;
                  return true;
                  }
               }
            

      
}

</script>


<form name="insertForm" method="post" action="insert.jsp"  onSubmit="return finalCheck()">
...........
...........
..........
 <input type="submit" name="insert" value="Insert" >
<input type="submit" name="show" value="Show">


My problenm is that if I will click on  the "Show" button it will also call the javascript and showing alert messages but I don't want it.
I only want it on "Insert" button.
can anubody suggest me the path.
Thanks in advance

0
Comment
Question by:m_sharma98
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 50 total points
ID: 9836297
1. change
function finalCheck(){
var formcount=0;
to
notSubmitting=false;
function finalCheck(){
  if (notSubmitting) return
var formcount=0;



and change
 <input type="submit" name="insert" value="Insert" >
<input type="submit" name="show" value="Show">

to
<input type="submit" name="insert" value="Insert" onClick="notSubmitting=false">
<input type="submit" name="show" value="Show"  onClick="notSubmitting=true">
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9836316
Change the Show button to this:
<input type="button" name="show" value="Show" onClick="this.form.submit()">

0
 
LVL 11

Expert Comment

by:Zontar
ID: 9836334

<script language="javascript">
window.doCheck = false;

function finalCheck(){
if(!window.doCheck)
  return true;

var formcount=0;
var cre = document.insertForm.credit.value;
var deb = document.insertForm.debit.value;
var button1=document.insertForm.insert.value;
var button2=document.insertForm.show.value;
alert("Button1 and Button2 are "+button1+"\n"+button2);

//if(button1 != button2){
     if (cre==0 || deb ==0)
     {
          alert("Sorry !!!\n\nYou can't insert zero values in database");
          return false;
               }
     else
          {
                   
          if (formcount == 0)
                {
                formcount++;
                return true;
                }
             }
         

     
}

</script>


<form name="insertForm" method="post" action="insert.jsp"  onSubmit="return finalCheck()">
...........
...........
..........
 <input type="submit" name="insert" value="Insert" onclick="window.doCheck = true;">
<input type="submit" name="show" value="Show">
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9836369
This

<input type="button" name="show" value="Show" onClick="this.form.submit()">

will not allow the form to be submitted if JavaScript is not enabled; also, you would have to change your serverside logic for determining which button was clicked.
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 10

Expert Comment

by:NetGroove
ID: 9836413
m_sharma98 can check that Insert button was NOT clicked.
0
 
LVL 1

Expert Comment

by:ferryl
ID: 9837407
You can try somthing like this:

<input type="button" name="insert" value="Insert" onclick="document.insertForm.submit()">
<input type="button" name="show" value="Show" onclick="yourFunction()">

change the input type="submit" to input type="button" and use the onclick function

0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9837452
Hello ferryl, you perversed my recommandation.
The button with the JavaScript submitting directly the form will NOT be handled by onSubmit form event.
So is my recomanadation to put the submit line in the Show button.

0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9837461
why not just have this, checkthe submit value if show return true

<script language="javascript">
function finalCheck(){
if(document.getElementById('submit').value.toLowerCase()=='show'){
   return true;
}

var formcount=0;
var cre = document.insertForm.credit.value;
var deb = document.insertForm.debit.value;
var button1=document.insertForm.insert.value;
var button2=document.insertForm.show.value;
alert("Button1 and Button2 are "+button1+"\n"+button2);

//if(button1 != button2){
     if (cre==0 || deb ==0)
     {
          alert("Sorry !!!\n\nYou can't insert zero values in database");
          return false;
               }
     else
          {
                   
          if (formcount == 0)
                {
                formcount++;
                return true;
                }
             }
         

     
}

</script>


<form name="insertForm" method="post" action="insert.jsp"  onSubmit="return finalCheck()">
...........
...........
..........
 <input type="submit" name="insert" value="Insert" >
<input type="submit" name="show" value="Show">


0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9839195
Huh?
getElementById gets elements BY ID
there are no elements with an id in your code, if there were, there would be TWO  and how would getElementById know which one was pressed?
A test of the event source would be more useful, but I think the code I posted (but did not test) is the simplest to figure out
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
updateParent() HTML Javascript 21 61
jQuery Animate Left Right 2 44
Safari SVG Image Problem 1 20
Hidden Field Value 10 37
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 …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now