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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read value of submit button 2

Status
Not open for further replies.

ksbigfoot

Programmer
Joined
Apr 15, 2002
Messages
856
Location
CA
I have two submit buttons (Close, Update).
The form calls a javascript function on the onsubmit event.

I am not sure how to read the value of the submit button that was pressed. The name of both submit buttons are "Submit".

Here is the onsubmit call that the form uses:
<code>
onsubmit="return validate(frmForm)"
</code>

Thanks.
 
well, you have two buttons with the same name... therefore, they are stored as an array when you retrieve the element. you'll need to loop through the array.

what are you trying to do, specifically?



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Howdy cLFlaVa,
I have a form that after they click on one of the submit buttons, I validate some of the fields in the form.
If they click on the "Close" submit button, I want to bypass the checking of the fields and return true. If they click on the "Update" submit button, I want to to check the fields.
Here is what I have so far for code:
<code>
<script language="Javascript">
function validate(frmForm) {
with (frmForm)
{
alert(Submit[0].value);
alert(Submit[1].value);
//if (Submit.value == "Close") {
// return true;
//}
if (!notblank(txtOne.value)) {
alert("One cannot be blank");
txtOne.focus();
return false;
}
}
return true;
}
function notblank(value) {
return (value != "")?true:false;
}
</script>

<form method="post" name="frmMyForm" action="Javascript2.asp" onsubmit="return validate(frmMyForm)">
<input type="text" name="txtOne" value="<%=strOne%>" id="txtOne">
<input type="Submit" name="Submit" value="Close">
<input type="Submit" name="Submit" value="Update">
</form>
</code>
 
the way i see it, you have two options.

1) set a parameter when you click one of the submit buttons, then check the value in the validate function.

2) make one of the buttons a "button" input type (instead of submit). have the onclick event call the validate function, then have the validate function call the form.submit() event.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Can't you just change the names of the submit buttons so they are different?


At my age I still learn something new every day, but I forget two others.
 
Scratch that. The name is not important, you cannot tell which button was pressed without an additional event like cLFlaVA suggested.

I say make your Submits type="button" and use an onclick to test and call form.submit as cLFlaVA said.


At my age I still learn something new every day, but I forget two others.
 
Howdy cLFlaVa,
I took your first suggestion. I created a hidden text field and I populated when I clicked the button.
It works perfectly. Star to you.

Howdy theniteowl,
If I changed the name of my submit buttons, how do I tell which one was pressed?

Thanks
ksbigfoot
 
niteowl,

one useful concept of naming two submit buttons the same thing is when it gets to server-side data mashing.

you can simply test for the value of the input named "submit". if the user clicked one, it'll have one value. if he clicked the other, it'll have a different value.

i won't even get into the reasons why nobody should name a form element using a keyword such as "submit" - that's for another day.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I have actually done that in the past (reading the Submit value from my ASP page) but gave it up as bad practice. :)


At my age I still learn something new every day, but I forget two others.
 
yeah - it can just as easily be accomplished with two submit buttons named different names...

in php:

Code:
if ( isset( $_POST['submitButtonOne'] ) ) {
    # do stuff when the first submit button is pressed
} elseif ( isset( $_POST['submitButtonTwo'] ) ) {
    # do stuff when the second submit button is pressed
} else {
    # no submit buttons were pressed, redirect back...
}



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I have a ton of pages in ASP. Where do I go to read up on the bad practices of reading the submit button value. That is how I am doing all my pages, I didn't know that was the wrong way of doing it. I would like to know the right way.

If you could point me in the right direction, I would really appreciate it.

Thanks
ksbigfoot
 
It's not an ASP bad practice, it's just that naming an element with a reserved word like Submit is a bad idea. You should always make your element names something not used by the browser or programming languages you are dealing with.
Also, since you are already using client-side code for validation it is just as easy to handle the buttons client-side also rather than having to test both values in your ASP page.

On the ASP side it does not matter really, you are just checking the posted value of a form element.

At my age I still learn something new every day, but I forget two others.
 
Thanks theniteowl,
I will keep that in mind when I am making my element names. I still need to learn quit a bit when it comes to Javascript. Star to you.

Thanks again,
ksbigfoot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top