through which submit button the form was submitted

S

stellstarin

I have a HTML page containing two submit buttons in the same form.When
the form is submitted,I want to know through which submit button the
form was submitted.
Is there any event or property which identifies this?
 
M

Madni

you can asign different IDs to both the buttons and invoke appropriate
methods for each button's onsubmit event ....
 
V

VK

I have a HTML page containing two submit buttons in the same form.When
the form is submitted,I want to know through which submit button the
form was submitted.
Is there any event or property which identifies this?

Unfortunately no - event model creators overlooked the variant of
multiple submit buttons in the form. onsubmit event contains FORM as
target/srcElement, so you cannot backtrace the pressed button.

Luckely click event is being processed before submit. So in your form:

<form action="foo.cgi" onsubmit="return validate(this)">
....
<input type="submit" name="act" value="Submit 1"
onclick="glbAct=this;">
<input type="submit" name="act" value="Submit 2"
onclick="glbAct=this;">
</form>

Now in validate() you can check global variable glbAct to find which
button was pressed.
 
S

stellstarin

as we are the middleware securing the backend application,we can't
able to change the html code in the backend application.
so we need to trace the details of the 'submit' button, when the form
submission was made .
(without adding anyother extra event handling ..like onclick())
 
R

RobG

as we are the middleware securing the backend application,we can't
able to change the html code in the backend application.
so we need to trace the details of the 'submit' button, when the form
submission was made .
(without adding anyother extra event handling ..like onclick())

In the HTML, give each submit button a different name and value.
Whichever one is clicked will send its name-value pair.

From the spec:

"If a form contains more than one submit button, only the
activated submit button is successful."

<URL:http://www.w3.org/TR/html401/interact/forms.html#submit-format>


You can add onclick attributes using script without modifying the HTML
at all (other than to add the script element of course), but don't make
your form submission script dependent.
 
A

arvindsd

If there are two submit buttons and your "method" attribute of your
form is "get" then the submitted buttons name and value appear as query
string parameters. Where as the one that is not submitted dont appear
in the query string.

This is possible when the button type is "submit".

Now if the button type is "image" then the workaround is different. I
was in a similar situation with some contraints. The constraints were -
no javascript to be used, no submit buttons but image buttons, and php
as the backend technology.

the HTML code was as below:
<Form method = post action="some.php">
<input type="image" name="n1[]" value="n1"/>
<input type="image" name="n2[]" value="n2"/>
</Form>

Now within "some.php" I can check which one of the image buttons was
clicked:

if($_REQUEST["n1"] == true){
//do something
} else if($_REQUEST["n2"] == true){
//do something else
}

In this case the names are treated as arrays. when the button is
clicked the array is populated.
$_REQUEST["n1"] == true checks that.
 
T

Thomas 'PointedEars' Lahn

Now if the button type is "image" then the workaround is different. I
was in a similar situation with some contraints. The constraints were -
no javascript to be used,

Nonsense. The two technologies are independent of one another.
[...]
<Form method = post action="some.php">
<input type="image" name="n1[]" value="n1"/>
<input type="image" name="n2[]" value="n2"/>
</Form>

Now within "some.php" I can check which one of the image buttons was
clicked:

if($_REQUEST["n1"] == true){
//do something
} else if($_REQUEST["n2"] == true){
//do something else
}

You were looking for

if (isset($_POST['n1']))

of course. And it is not necessary that the name ends with '[]'.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top