Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which Submit?? 1

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
I have a form, which, onSubmit() calls a validation function. However, I have two submit buttons ( both with the same name but different values ). In my validation function, I need to carry out an extra step if one of the buttons is pressed. How do I determine in my validation function which of the buttons was pressed?
 
I don't really want to change the name of the submit buttons if I can help it. The submit passes the contents of the form to an ASP page. In the ASP page, I have a case statement which carries out different options depending on the value of the submit.

Is there no other way. If I do as you suggest, how do I test what button was pressed? Mise Le Meas,

Mighty :)
 
To see what button was pressed, you would just send another variable to the validation function (0 or 1, for instance).

You could then set a hidden form variable to a certain value depending on what button was pressed (again, 0 or 1 would work great), and then just check that value on the subsequent page to see what action you wish to take.

hope it helps!:)
Paul Prewett
 
Maybe this is more complicated than I thought. This is what happens. I have two submit buttons - "Add another product" and "submit order". When either button is pressed a client side validation program is run and if it passes the form contents are passed to an ASP page where certain actions are carried out depending on which submit button is pressed.

However, what I would also like to do is that when the user presses the "submit order" button, I would like to be able to pop up a confirm window after validation is complete. This I need to check first if it was the "submit order" button and not the "add another product" button that was pressed. If the user chooses OK on the confirm then the form data is sent to the ASP page.

I hope that's clearer. Mise Le Meas,

Mighty :)
 
Ok, so have two onClick events that call a submit function -- each button would send one of two variables to the submit function. Also add one hidden element to your form. For example
Code:
<input type=hidden name=hiddenElementName>

<input type=submit name=submit value=&quot;Submit Order&quot; onClick=&quot;submitForm(0)>
<input type=submit name=submit value=&quot;Add another Product&quot; onClick=&quot;submitForm(1)>
Then, your submit function would resemble this:
Code:
function submitForm(value){
  if (value == 0){
    document.formName.hiddenElementName.value = 0;
    document.formName.submit();
  }
  else{
    document.formName.hiddenElementName.value = 1;
    document.formName.submit();
  }
}
Now that the hidden element is set to either 0 or 1, it should be easy enough to check (inside the actual form validation function) and see which button what pressed by simply evaluating the value of that element, right?

The validation function would still be called with no further coding because your form had been submitted.

good luck!:)
Paul Prewett
 
this may be, and indeed probably is, a silly question %-) but what is the actual purpose of the hiddenElement as it seems that this would work without it?
m Mark Saunders :)
 
The action page checks the value of the hidden element to determine what to do with the form input. Mise Le Meas,

Mighty :)
 
so why is it nceessary when a variable 0 or 1 is passed?

Code:
if (value == 0){

surely this is sufficient to direct the flow of the program to submit the relevant form(s)?

m Mark Saunders :)
 
You might have an action page that gets called regardless of whether the value passed was 0 or 1. If you want that action page to carry out different tasks on the form input depending on which submit was pressed, how do you do this without setting some kind of hidden field. Mise Le Meas,

Mighty :)
 
duhhh! cheers for your patience - i'm just reviewing an article on hidden fields now.
thanks! Mark Saunders :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top