1

I have created one HTML form which consists of multiple buttons of "edit" and "delete". I put them in a for loop in order to view many of them on another page.

How can I get the name field of the submit button, or whichever button I have clicked, on the next JSP page? Or, can you can provide me an alternative for this?

<% for (int t = 1; t < 10; t++) { %>
    <label>
        <input type="submit" name="<%=t %>" value="edit" onclick="this.form.action='df1.jsp?flag=1&act=1';this.form.submit();" />
        <input type="submit" name="<%=t %>" value="delete" onclick="this.form.action='df1.jsp?flag=1&act=1';this.form.submit();" />
    </label>
<% } %>

When I view the page source, it's showing the name="1" for edit and name="1" for delete till 10.

I want to get the value of name field on server side. I also can't apply request.getParameter(), because I am not aware which button will user hit.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1950705
  • 41
  • 2
  • 6

4 Answers4

0

Use a hidden tag, which stores the value of the clicked button

<input type="hidden" name="hidBt" />

Place this hidden tag outside the value.

<label>
    <input type="submit" name="<%=t %>" value="edit" onclick="this.form.hidBt.value=this.value;this.form.action='df1.jsp?flag=1&act=1';this.form.submit();" />
    <input type="submit" name="<%=t %>" value="delete" onclick="this.form.hidBt.value=this.value;this.form.action='df1.jsp?flag=1&act=1';this.form.submit();" />
</label>    

After that call the hidden variable

String butValue = request.getParameter("hidBt");
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
0

As you're already changing the form action URL in the onclick (which is a very poor practice, but that aside), why not just adding the row identifier along as well?

<input type="submit" name="<%=t %>" value="edit" onclick="this.form.action='df1.jsp?flag=1&act=1&edit=<%=t%>';this.form.submit();" />
<input type="submit" name="<%=t %>" value="delete" onclick="this.form.action='df1.jsp?flag=1&act=1&delete=<%=t%>';this.form.submit();" />

This way you can just do:

String edit = request.getParameter("edit");

if (edit != null) {
    // Edit button of row "edit" was invoked. Do your job here.
}

String delete = request.getParameter("delete");

if (delete != null) {
    // Delete button of row "delete" was invoked. Do your job here.
}

Otherwise you'd need to prefix the button name with some fixed identifier:

<input type="submit" name="edit_<%=t %>" value="edit" onclick="this.form.action='df1.jsp?flag=1&act=1';this.form.submit();" />
<input type="submit" name="delete_<%=t %>" value="delete" onclick="this.form.action='df1.jsp?flag=1&act=1';this.form.submit();" />

So that you can just do:

for (int t = 1; t < 10; t++) {
    if (request.getParameter("edit_" + t) != null) {
        // Edit button on index "t" was invoked. Do your job here.
        break;
    }

    if (request.getParameter("delete_" + t) != null) {
        // Delete button on index "t" was invoked. Do your job here.
        break;
    }
}

Unrelated to the concrete problem, writing JSPs this way with scriptlets <% %> is since a decade considered oldschool and a poor practice. See also How to avoid Java code in JSP files?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

There are a couple of approaches you could take to this.

Look at all the submitted fields

  • Get all the fields that were submitted (as per this question)
  • Loop over them
  • Pattern match their names until you find one that matches the naming convention you have for your submit buttons

Look for all possible submit button names

  • Generate a list of all the submit button names you could have
  • Loop over that list attempting to get a parameter with each name until you find one

Given your names are numeric, the number of possibilities is possibly not finite. So the first approach is probably better.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1
you can get button using 

String buttonName =  request.getParameter("button-name") 

Then you have to pass the value as hidden value to next page;

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37