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
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 want to make a textbox read only or disable if sql query retrieves no rows as result. What shall i write to make the textbox "text1" disabled/read only
<%
Set ObjRs=ObjConn.Execute(strSQL)
if not ObjRs.EOF then
CMSPI = ObjRs("CMSPI")
else
< Here i need to disable / make read only the combo box>
end if
%>
<HTML>
<Table>
<form method="POST" action="Dis****.asp" name="Form1">
<Tr>
<Td>
<input type="text" id="idtext1" name="text1" />
</td>
</tr>
</table>
</html>
You could probably make it work if you use something like this
document.getElementById("idtext1").style.display = "none";
<%
Dim isDisabled
Set ObjRs=ObjConn.Execute(strSQL)
if not ObjRs.EOF then
CMSPI = ObjRs("CMSPI")
else
isDisabled = " disabled"
end if
%>
<HTML>
<Table>
<form method="POST" action="Dis****.asp" name="Form1">
<Tr>
<Td>
<input type="text" id="idtext1" name="text1" / <%= isDisabled%>>
</td>
</tr>
</table>
</html>
i would like to know how to disable/hide all the buttons once user clicks on any of the buttons. I am only allowed to use c# or html. I have found solutions in Javascript but i cannot use it. (i did not upload my razor c# code due to lack of space)
I am creating a program that allows user to vote for any one of the candidates. Once user has clicked and chosen on one candidate, the voting result will be displayed and user should not be allowed to vote again.
<!DOCTYPE html>
<html>
<head>
<title>Elections</title>
</head>
<body>
<form id="form1" method="post">
<div>
<table>
<tr>
<td>Harry</td>
<td><input id="Harry" name="submit" type="submit" value="Vote Harry" /></td>
</tr>
<tr>
<td>John</td>
<td><input id="John" name="submit" type="submit" value="Vote John" /></td>
</tr>
<tr>
<td>Bryan</td>
<td><input id="Bryan" name="submit" type="submit" value="Vote Bryan" /></td>
</tr>
<tr>
<td>Jack</td>
<td><input id="Jack" name="submit" type="submit" value="Vote Jack" /></td>
</tr>
</table>
</div>
<div>
#if (result != "")
{
<p>#result</p>
}
<!--Ensure that user has voted before showing the vote results-->
#if (voteCheck == true)
{
<p>Total Votes: #Session["totalVotes"]</p> <!--using session allows values to be kept even after button click-->
<p> Harry: #Session["Harry"]</p>
<p> John: #Session["John"]</p>
<p> Bryan: #Session["Bryan"]</p>
<p> Jack: #Session["Jack"]</p>
}
</div>
</form>
</body>
</html>
You need an action attribute to the form action="youraction/controller here"
And in youraction you write some C# code return list of display value ("none" or "")
And in cshtml you add style="display:#display"
<input id="Harry" name="submit" type="submit" style="display:#display" value="Vote Harry" />
You could simply add a Ternary operator that check the session object and sets the disabled property accordingly.
<input id="Harry" #(Session["Harry"] != null ? "disabled" : "") name="submit" type="submit" value="Vote Harry" />
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.
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.