How to get value from submitted button in JSP? - html

Don't know a better title but here is what im trying to do.
I have the following form where which will display a list of books and after every book entry there is a remove button. The list can be any length. In order to get the book id I have set name="remove[${cartItem.bookId}]" but how do I get this value in a servlet? request.getParameter("remove") and request.getParameterValues("remove") returns null everytime.
<form method="post" action="removeBookFromCart">
<c:forEach var="cartItem" items="${sessionScope.cart.cartItems}">
<tr>
<td><c:out value="${cartItem.title}" /></td>
<td><input type="submit" name="remove[${cartItem.bookId}]" value="Remove"/></td>
</tr>
</c:forEach>
</form>

There are several solutions:
Use <button name="bookToRemove" value="${cartItem.bookId}" type="submit">Remove</button>. This, however, doesn't work as specified in IE6 and IE7 (at least).
Iterate through the parameters, find the one which starts with remove[, and extract the ID after that. You could use a simpler name, such as remove_${cartItem.bookId}
Create one form per cart item, instead of a global form, and use a hidden field to contain the book to remove.
The third way is probably the easiest one.
You could also use some JavaScript to initialize the value of a hidden field when a button is clicked, but this is not easier than the methods above, and requires JavaScript.

You have to use hidden parameters:
<input type="hidden" name="remove" value="${cartItem.bookId}"/>
Let the submit button be just this way:
<input type="submit" value="Remove"/>
EDIT:
Yes, you have to create a form element for each book:
<c:forEach var="cartItem" items="${sessionScope.cart.cartItems}">
<tr>
<td><c:out value="${cartItem.title}" /></td>
<td>
<form method="post" action="removeBookFromCart">
<input type="submit" value="Remove"/>
<input type="hidden" name="remove" value="${cartItem.bookId}"/>
</form>
</td>
</tr>
</c:forEach>

Related

unable to fetch selected row data from jsp to servlet using hidden element

I want to perform CURD operation using JSP,servlet and Mysql . Trying to post id using hidden element but when i perform delete operation it always deleted the first row in the table.
Please let me know how to post selected data to servlet data using single form through hidden element.
<c:forEach var="record" items="${SLIST}">
<input type="hidden" value="${record.Id}" id="poststudentId" name="poststudentId" />
<td>${record.Id</td>
<td>${record.lastName}</td>
<td><input type="submit" value="View" id="view_button" name="option" class="view" /></td>
<td><input type="submit" value="Edit" id="edit_button1" name="option" class="Edit" /></td>
<td><input type="submit" value="Delete" id="delete_button" name="option" class="Delete" /></td>
</tr>
</c:forEach>
Taken literally, this appears to need an HTML <form> tag surrounding the <input> elements, otherwise it can't relate them together.

Bulk delete with checkboxes and additional values

So I think I'm in some kind of tunnel-vision right now.
I have to maintain a very basic application (HTML + ASP). In this application, there is a small table with some values:
<table>
<tr>
<td>Customer-ID</td>
</tr>
<tr>
<td>Waregroup</td>
</tr>
<tr>
<td>Some other ID</td>
</tr>
<tr>
<td>[edit]</td>
</tr>
<tr>
<td>[delete]</td>
</tr>
<tr>
<td>[checkbox for deletion]</td>
</tr>
</table>
It looks like this, to make it more visual (excluded the rows with data, they are not needed for this):
Now, the delete-function just doesn't take simply the customer-id (KD Nr.), it takes several more arguments, which are provided in a link. For example, this here:
someSite.com?sid=123456&gkid=133&kdid=9043168&wws=9&del=1
Some of the values are taken from the QueryString, other from a ResultSet from a SQL-Query.
Now I had to extend this application and add a checkbox to bulk-deletion. This wasn't a problem at all, but now I'm kinda stuck on how to get the values which are required for the deletion, when I can basically just use 1 value in each checkbox. I thought about using hidden inputs, but then I have no idea how to determine, which "rows" should be deleted and which not:
This is the addtion I made:
<td>
<input type="checkbox" name="deletebox[]" value="0">
<input type="hidden" name="sid" value="<%=Request.QueryString("sid")%>">
<input type="hidden" name="gkid" value="<%=Request.QueryString("gkid")%>">
<input type="hidden" name="kdid" value="<%=rs("ka_kdnr")%>">
<input type="hidden" name="wws" value="<%=rs("ka_sysid")%>">
</td>
My idea is to loop through every checked checkbox, take the values from the hidden fields and run my deletion-script in a loop.
Any lead on how to do so? Can I simply iterate over all checked checkboxes and request for the current value of the hidden fields?
I basically just need the gkid and the gkkid for the stored procedure to work.
If you submit a form which contains multiple instances of the same form variable then that the value of that variable in the Request object will appear as a comma separated array. In other words if your form contains the following:
<input type="checkbox" name="gkid" value="25">
<input type="checkbox" name="gkid" value="50">
<input type="checkbox" name="gkid" value="75">
Then Request("gkid") will display a value of "25,50,75" if you check all of them. You can then split your array as follows.
For i = 1 to Request("gkid").Count
Response.write Request("gkid")(i) & "<br>" & vbcrlf
Next
I'm going to assume that you can take it from there and adapt the above to use Request.Form("gkid")(i) and apply logic inside the loop with code which calls your stored procedure.
Your question mentions a variable called "gkkid". I can't see where that comes from. If I could I might be able to improve on this answer.

How to add checkboxes to a list in html/django

I am developing a django/html application where I have a table of data. I have to make a way for my users to delete multiple rows in a table. Therefore, I have decided to add checkboxes in a list.
I know that I can include it as
<tr>
<td><input type="radio" name="item1" /></td>
<td>Item1</td>
</tr>
<tr>
<td><input type="radio" name="item2" /></td>
<td>Item2</td>
</tr>
for each item. Then in the end, I can add:
<input type="submit" name="delete" value="Delete Items" />
But this will mean that I will have to enclose my list within a <form></form>
Is this an ethical way of doing it?
I want to add this feature to my site but I also want to do it in the most professional way. Can anyone tell me if I am going in the right direction?
Since you use Django, one way would be to take advantage of what Django provides for forms.
Here are the examples from the official doc, for version 1.10:
-for the radio buttons:
https://docs.djangoproject.com/en/1.10/ref/forms/widgets/#widgets-inheriting-from-the-select-widget
-for the form:
https://docs.djangoproject.com/en/1.10/topics/forms/#building-a-form-in-django
A django form uses the form tag.

Proper way to include multiple forms in a single table

Is there a correct way to include multiple forms in a single table?
For example, I have 8 rows, four of them correspond to a single table, the other four correspond to a second. What would be the best way to do this? I know putting a table between TRs is invalid.
I think I found the solution for your problem, since I found myself on the same situation. Here is an example of what I did.
<form id="uno" name="uno" action="somescript.php" method="post">
<input type="hidden" id="myhidden" value="1">
</form>
<form id="dos" name="dos" action="anotherscript.php" method="post">
<input type="hidden" id="myhidden" value="2">
</form>
<table id="uniqueTable">
<tr>
<td><input form="uno" type="text" id="x_value" name="x_value" value="0.00"></td>
<td><input form="dos" type="text" id="x_anothervalue" name="x_anothervalue" value="0.00"></td>
</tr>
</table>
In this way, although there is only one table each of your inputs will belong to the form stated with the form="formName"
Hope it helps someone
- anakin
I don't think there is a standard way to do this. (which sucks)
You'll have to do something like give all the form elements in a row a prefix to indicate which row they are in, then detect which button was pressed. On the server you can then discard all the values that you aren't interested in.
edit: The information (name and value) of the buttons not pressed won't be submitted)
(Alternatively, if you use javascript you should be able to make work around to collect all the values from that row's inputs when the button is pressed.)

Intermixing HTML form and table

For a standard "add item" form page it is desirable to have two submit buttons: an "OK" button and a "cancel" button, where the former POSTs the form to one URL, and the latter GETs some other URL.
This obviously means that two separate FORMs are needed, and, if laid out with tables, the markup would go as follows:
<form action="add.html" method="post">
<table>
<tr>
<td>Enter data:</td><td><input type="text" name="data"/></td>
</tr>
</table>
<input type="submit" value="OK"/>
</form>
<form action="index.html" method="get">
<input type="submit" value="Cancel"/>
</form>
However, this would result in the two buttons being placed below each other. It would be desirable to have them placed side by side. The following works:
<form action="add.html" method="post">
<table>
<tr>
<td>Enter data:</td><td><input type="text" name="data"/></td>
</tr>
<tr>
<td><input type="submit" value="OK"/></td>
</form>
<form action="index.html" method="get">
<td><input type="submit" value="Cancel"/></td>
</tr>
</table>
</form>
But although I've seen it used on commercial websites, I guess it's not quite legal HTML.
So thus:
1) Since the second methods works, are there any good reasons for not using it?
2) Are there any better solutions?
EDIT: This was a silly question. The second method is unnecessary. Solution: add to the first method a CSS rule of:
form
{
display: inline;
}
You broke my mind.
There are many and varied problems with what you have here, but I'll start by pointing out that Cancel/Reset are not considered good things generally.
I'll follow that by pointing out that you could use CSS to style the buttons side by side in your first example, and follow that by pointing out that a simple type="button" could have any arbitrary script attached to it to do your cancel navigation, and follow that by the fact a simple anchor tag would be even more straightforward.
And I'm not going to mention the table, because that'll just start some trouble.
Don't use a second form. Wrap both buttons in the same form, and do something like this with the cancel button:
<input type="button" text="Cancel"
onclick="document.location.href='index.html';return false;" />
1) When you create a page using "legal HTML," you can have an expectation that what works in today's browsers will work in tomorrow's browsers, or in some other user agents that you might not have checked the site in. But in the example you've given, the degree to which different browsers agree on how to "fix" the HTML for display is much less certain. It adds a level of predictability to the how the page will display when "valid HTML" is used. Plus, who knows how a user agent such as a screenreader would describe the code in question.
2) Is using a regular anchor tag an option?
<td><input type="submit" value="OK"/></td>
<td> or Cancel</td>
Or you could use CSS to move a second form and its submit button up into the first form, but the specifics of this might be tricky.
Add a row to your table
<tr>
<td><input type="button" value="Cancel" onClick="window.location='./index.html'"/></td>
<td><input type="submit" value="OK" name="submit"/></td>
<tr>