user3240544
user3240544

Reputation: 267

2 submit buttons - different queries,

I have a simple question and I can't find the answer by myself, so here it is.

Let's assume I have one simple index.html in which I have a form with 2 submit buttons. The action to those buttons is mapped to action.jsp. What I want to do is, if I click on the first submit button I want a query of type "SELECT * FROM USER WHERE ID = 1" and when I click on the second submit button I want a query of type "SELECT * FROM USER WHERE ID = 2". Can i do that in a single JSP file? How to do the check whether the first or the second submit button is pressed in the action.jsp - is that possible? Thanks!

Upvotes: 0

Views: 462

Answers (3)

Java Enthusiast
Java Enthusiast

Reputation: 664

Yes you can do both the operations on same JSP page. Lets say you have two buttons like :

<input type="submit" name="bt1"/>
<input type="submit" name="bt2"/>

Now you can check their button click like :

if(request.getParamater("bt1")!=null)
{
     //your first query
}
if(request.getParameter("bt2")!=null)
{
    //your second query
}

Also make sure that form action should be the same JSP page on which it is declared

Upvotes: 2

Yaje
Yaje

Reputation: 2831

try using 2 forms, 1 form for each button.

or try using button only and create a function to handles it onclick()

Upvotes: 1

rachana
rachana

Reputation: 3424

Yes , you can use two submit buttons in singleJSP page.

Try this

<INPUT type="submit" name="bsubmit" value="Submit 1">

<INPUT type="submit" name="bsubmit" value="Submit 2">

Now when you create the script, these unique values will help us to figure out which of the submit buttons was pressed.

For example

Following is a sample Active Server Pages script to check for the submit button.

if Request.Form("bsubmit") = "Submit 1" then
btnID = "1"
elseif Request.Form("bsubmit") = "Submit 2" then
btnID = "2"

At server page you can write something like this

Upvotes: 0

Related Questions