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

How to differ between a submit-button and form.submit()

Status
Not open for further replies.

BennySHS

Programmer
Mar 15, 2005
32
DE
Hi there,

got a little prob. and i hope you can help me!

I got a form with a selectbox and some submit-buttons.
If somebody changes the selectbox, the form should be sent with onChange="form.submit();".

The problem:
How do I know if somebody changed the selectbox or pressed a button ?
thx a lot,
greets,
ben
 
There are ways, but it would help to know what you want to do with the info.

Quick idea: Create a hidden form element. Call it submitItem. Change your onChange statement to:

onChange='document.forms[0].submitItem.value="select";form.submit();'

Add an event to your SUBMIT button : onClick='document.forms[0].submitItem.value="button";'

I haven't tested these.

Good luck.

--Dave
 
What would be the need to know the difference if both result in the submission of the form?

You could change your submit buttons to type="Button" and use an onclick to execute whatever function you want to occur and use the form.submit() within that function.

There are a number of ways to handle it, it really depends on what you need to occur and how it differs between the two possible ways of submitting.

Paranoid? ME?? WHO WANTS TO KNOW????
 
Hey,

thx a lot for your replies, it works now.
I use a hidden field which only gets a values if the form is send by the changes on the selectbox. So if the hidden field is empty, the selectbox was not used.

I need this because if somebody changes the selectbox, an other selectbox should change the values.
And if the somebody submit the form by pressing the button, a new recordset is created...

thx again,
greets ben
 
You don't need to have a hidden field for this. In fact, you don't need anything extra client-side to do this.

If the submit button is used, its name and value are passed through to the server. If the form was submitted programatically, this will not be the case.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,

Do you mean that if the receiving program were a JSP and a Submit button on a form was used to submit that form like:
Code:
<input type='submit' name='mySubmitButton' value='1' />
...then in the JSP, you can write:
Code:
String buttonVal = request.getParameter("mySubmitButton");
...and if the button was used, buttonVal will be "1" and otherwise it would be null?

--Dave
 
Not being a JSP programmer, or having a JSP debugger in front of me, I cannot tell you what the value would be if the button were not pressed.

If it is pressed, I believe (but again, cannot test) the name and value are passed just like any other form element.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Seems like you're right. Here is my little test:

file: testSubmit1.jsp
Code:
<%@ taglib uri="jruntags" prefix="jrun" %>
<html>
<body>
<form method='POST' action='testSubmit2.jsp'>
<input type='button' name='myRegularButton' value='Submit Manually' onclick='document.forms[0].submit();' />
<input type='Submit' name='mySubmitButton' value='Submit Automatically' />
</form>
</body>
</html>

file:testSubmit2.jsp
Code:
<%@ taglib uri="jruntags" prefix="jrun" %>
<%
	String valReg = request.getParameter("myRegularButton");
	String valAuto = request.getParameter("mySubmitButton");
%>
<html>
<head>
<script>
alert('<%=valReg%> || <%=valAuto%>');
document.location = 'testSubmit1.jsp';
</script>
</head>
<body>
</body>
</html>

When the type='button' button is used to submit the form, the alert on the second page comes back as null || null. But when I submit using the type='submit' button, it reads null || Submit Automatically.

You're right, too, that I don't know when I'll ever need this, but apparently BennySHS (Ben) is using it as a shortcut to determining whether he needs to do a database UPDATE or INSERT (or something like that).

Thanks, Dan!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top