Link to home
Start Free TrialLog in
Avatar of m_sharma98
m_sharma98

asked on

about onSubmit javascript

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

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NetGroove
NetGroove

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


<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">
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.
m_sharma98 can check that Insert button was NOT clicked.
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

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.

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


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