Solved

about onSubmit javascript

Posted on 2003-11-28
9
4,764 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

749 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