Solved

about onSubmit javascript

Posted on 2003-11-28
9
4,758 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now