Solved

about onSubmit javascript

Posted on 2003-11-28
9
4,763 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 48
iframe - style elements within an iframe 7 36
Do alert on select 6 13
Using toggleClass to resize Bootstrap columns 7 15
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

790 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