userdetails.jsp
<tr>
<td>
<%
out.println(rs.getString(1));
name=rs.getString(1);
out.print("<input type='hidden' name='user' value='"+name+"'");
%>
</td>
<td>
<%out.println(rs.getString(2));
%>
</td>
<td>
<%out.println(rs.getString(3));
%>
</td>
<td>
<%out.println(rs.getString(4));
%>
</td>
<td>
<input type="Submit" name="delete_user" value="Delete"/>
</td>
</tr>
when i click the delete button only first row is getting deleted and not the row corresponding to which button is clicked
You're putting multiple hidden input values and delete buttons altogether in the same form. When you use request.getParameter() to get the hidden input value, you will indeed only get the first one, regardless of the delete button pressed.
You need to group the hidden input and the delete button in their own forms.
<td>
<form action="delete" method="post">
<input type="submit" name="delete_user" value="Delete" />
<input type="hidden" name="user" value="<%=rs.getString(1)%>" />
</form>
</td>
This way the request will always have the one and the right user name as parameter.
Said that, using scriptlets in JSP is 90's way of writing JSPs and also mingling database logic in a JSP is not really a good practice. I'd suggest to get yourself through this answer.
You can add the user name to the button value upon clicking it:
<input type="Submit" name="delete_user" value="Delete" onclick="this.value += ' <% out.print(name); %>'; this.disabled = true; " />
Then in the server side code, parse the value: get the text after first space, which is the user name to delete and reference only row with that user name.
Related
I have a table, which gets filled at Runtime. In the last Row I want to add one button to each Column and when one button is pressed it shall Link to the same Post method, but with a different value.
<form method="post" asp-action="ChangeAll">
<input asp-for="DataId" type="hidden" />
<table>
<thead>
.....
</thead>
<tbody>
....data dynamically loaded
(
<td>
<input type="checkbox" name="boxes" value="#Model.RowId" />
</td> )
//last Row:
<tr>
<td>
<input type="submit" value="Update" />
<input type="hidden" name="selectedAction" value="#MyEnum.Value1" hidden />
</td>
......
<td>
<input type="submit" value="Update" />
<input type="hidden" name="selectedAction" value="#MyEnum.Value20" hidden />
</td>
</tr>
</tbody>
</table>
</form>
[HttpPost]
public async Task<IActionResult> ChangeAll(int[] boxes, int DataId, string
selectedAction){
The problem with the above is that no matter what button I click, it always posts back the value of the first button (MyEnum.Value1).
I have tried to add an asp-action="ChangeAll" to each button, but the behaviour stays the same.
So, how can I get the value of the Button I actually clicked?
In your code, All the hidden input have the same name, So when you submit the form. It will bind the first one. You can change your code like this:
<form method="post" asp-action="ChangeAll" id="Form">
.....
<input type="hidden" name="selectedAction" id="select" hidden />
<tr>
<td>
<input type="submit" value="Update" data-action="#MyEnum.Value1" onclick="choice(this,event)"/>
</td>
........
<td>
<input type="submit" value="Update" data-action="#MyEnum.Value20" onclick="choice(this,event)"/>
</td>
</tr>
</form>
<script>
function choice(a,event){
event.preventDefault();
var result = $(a).data("action");
document.getElementById("select").value = result;
document.getElementById("Form").submit();
}
</script>
In the above code, I only wrote one hidden input and customized an attribute--data-action to accept #MyEnum.Valuexx's value, and then assigned this value to the hidden input.
I am trying to pass a value which was retrieved from database to a jsp page using a HTML form, but i think am failing to pass the result set value into the hidden input tag which sends to next page.i have included code related to the question.please help
inv.jsp: This is the code which retrieves data from data base and it displays table table perfectly no problem there.
while(resultset.next()){ %>
<TR> <% int gen=resultset.getInt(1); %>
<TD> <%= gen %></TD>
<TD> <form action="invgen.jsp" method="get"><input name="gen" type="hidden"><input value="Generate" type="submit"></form></TD>
</TR>
invgen.jsp: This part is failing to get the parameter passed by form.
String invno = request.getParameter("gen");
if(invno!=null){
String query="SELECT invno,invdate,mode,candidate,invamnt,sapid,DOJ,fctc,locn,band,skill,srvctax,sbc,total,gtotal from allinvoice where invno='"+invno+"'";
statement=conn.createStatement();
resultset=statement.executeQuery(query);
change code like this.
<TD>
<form action="invgen.jsp" method="get">
<input name="gen" type="hidden" value="<%=resultset.getInt(1)%>">
<input value="Generate" type="submit"></form>
</TD>
You must have to pass value in hidden field to get value in form action page as below.
<TR>
<% int gen=resultset.getInt(1); %>
<TD> <%= gen %></TD>
<TD>
<form action="invgen.jsp" method="get">
<input name="gen" type="hidden" value="hiddenvalue">
<input value="Generate" type="submit">
</form>
</TD>
</TR>
I cannot type code but you should put the resultset in the request as a parameter.
In the next page you have access through request.getParameter("nameOfObject");
By the way: use a servlet to Query a Database an use the dispatcher to forward the result to the specific page.
Hope this helps.
Thanks to #AJ93 solved my problem. just had to do a minor tweak inside for
<input name="gen" type="hidden" value="<%= resultset.getInt(1) %>">
it works perfectly.
i have a jsp page . i display books from database. in front of all book i have order button. I want to order book . When i order bookcode and bookname will add
to database border table. but i cant get data from html table. How i can get data from html table in jsp file?
st =conn.prepareStatement("select * from books where book_name like ? ");
st.setString(1, "%"+book+"%");
ResultSet rslt = st.executeQuery(); if(rslt.next()){ %>
<h3 align="center">Result Of Your Search </h3>
<table align="center">
<thead>
<tr>
<td>Book Name</td>
<td>Author</td>
<td>Title</td>
<td>Code</td>
<td>Page</td>
</tr>
</thead>
<tbody>
<% do {
%><tr>
<td><%=rslt.getString("book_name")%></td>
<td><%=rslt.getString("author")%></td>
<td><%=rslt.getString("title")%></td>
<td><%=rslt.getString("code")%></td>
<td><%=rslt.getString("page")%></td>
<td><form method="POST" action=book.jsp>
<input type="submit" value="order" name="order" /></form></td>
<% } while (rslt.next()); %>
</tbody>
</table>
'
i want when i click to order button book name and book code will add to order database table
You can do it with two ways;
Ajax Post Method (recommended and popular way)
You can create a js which calls http post action to book.jsp and save your order without change page. You have to learn how to post with ajax.
Form Post Method (ehhh way)
When you create your table you have to define different forms with hidden input areas:
<% do {
%><tr>
<form method="POST" action=book.jsp>
<td><%=rslt.getString("book_name")%></td>
<td><%=rslt.getString("author")%></td>
<td><%=rslt.getString("title")%></td>
<td><%=rslt.getString("code")%></td>
<td><%=rslt.getString("page")%></td>
<td>
<form method="POST" action=book.jsp>
<input type="hidden" value="<%=rslt.getString("book_name")%>" name="book_name" />
<input type="hidden" value="<%=rslt.getString("author")%>" name="author" />
<input type="hidden" value="<%=rslt.getString("code")%>" name="code" />
.
.
.
<input type="submit" value="order" name="order" /></form></td>
</form>
<% } while (rslt.next()); %>
Then you can use getParameter("book_name");
OR
Just add:
<input type="hidden" value="<%=rslt.getString("id")%>" name="id" />
And you can;
`String id = request.getParameter("id");`
//Insert DB
I recently decided to make a little game, and I'm trying to design the first page, to either create an account or go to an existing account.
Here's what I have:
<center>
<table>
<tr>
<td>
<form action = "findMyAccount.html" method = "post">
<input type = "submit" value = 'Login to an Existing Account' />
</form>
</td>
<td>
<form action = "accountMaker.html" method = "post">
<input type = "submit" value = 'Make a new Account'/>
</form>
</td>
</tr>
</table>
</center>
But when I click the "Make a new Account" input, it takes me to the findMyAccount.html instead of accountMaker.html. I'm not sure what's going wrong, I'm still a newbie when it comes to this stuff, so bear with me. Thanks
I observed your code and I think you must remove backslash at the end of input tag,that might solve your problem.
Solution:
do this instead the last line code
<input type="submit" value='Make a new Account'>
instead of
<input type="submit" value='Make a new Account'/>
I'm trying to post some field data from my HTML table to a new page, but I can't seem to get my hidden input value to be anything other than null. Every row in the table has a Details button that I want to post just the primary key from that table to the posted page. Here's what I've tried that doesn't work:
<tr VALIGN="top">
<td>
<input type="submit" value="Details" />
<input type= "hidden" name = "post" value=<%rs.Fields(1).Name%>/>
</td>
<% For I = 0 to rs.fields.count - 1 %>
<td BORDERCOLOR="#c0c0c0"><font SIZE="1" FACE="Arial" COLOR="#000000"><%=(rs.Fields(I).Value) & " "%><br></font>
</td>
<%next%>
I've also tried a few other variations, but I'm sure there's a simple way to do this, I couldn't figure it out. If you need more info let me know, thanks
EDIT:
rs.Fields(1).Name has a value when I do Response.Write directly above the posted code, so I know that the result is not null.
I think you are missing the Response.Write or in a short form <%= %>
(You still need to output the value to the HTML!)
<input type= "hidden" name = "post" value="<%= rs.Fields(1).Name %>" />
I'm not sure if it will be affecting it, but try it without the spaces around the "=":
<input type="hidden" name="post" value=<%rs.Fields(1).Name%> />