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%> />
Related
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 am creating a web page that displays a table. It also has four columns at the end of each record that are text fields where the user can enter data. When the user hits the submit button at the bottom I want to save all the data in all the text fields and add it to my table. How can I save the data for all the text fields? Here is my code.
<h1>Testing Table</h1>
<form action="/table/" method="POST">
<table>
<thead>
<tr>
<% event_column.each do |header| %>
<th>
<%= header %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% events.each do |event| %>
<tr>
<% event = event.first(14) %>
<% event.each do |key, value| %>
<td>
<%= value %>
</td>
<% end %>
<td><input type="text" name="event_description"></td>
<td><input type="text" name="event_type">
<td><input type="text" name="event_class">
<td><input type="text" name="issue_expert"></td>
</tr>
<% end %>
</tbody>
</table>
<br/>
<input type="submit">
<% ActiveRecord::Base.clear_active_connections! %>
</form>
I understand my issue. Since I am using the same variable to write all of the user entered columns when I try displaying in my POST method
<td><%= params[:event_description] %></td>
It will only display the last value entered because it's being reused. Is there anyway that when I hit submit I can loop through all the html text fields and save all the data? I'm pretty stuck here and I've searched all around. I understand how to save the text entry for the last row but, I don't know how to save all the text entries. I'm new to sinatra so I must be doing something fundamentally wrong.
By default when Sinatra (using Rack) parses the form data then if there are any duplicate keys then the last one is used and overwrites the others. However if you use a name that ends with [] then Sinatra instead creates an array containing all the entries with that name.
So in your case you need to change the names of the input elements:
<input type="text" name="event_description[]">
(and similarly for the others). Note the [] at the end of the name.
Now when you submit the form, params['event_description'] will be an array containing all the items submitted to the event_description input elements.
The Sinatra/Rack query parsing is actually more sophisticated than this, so you can go further with your form. In your loop you could do something like this:
<td><input type="text" name="events[][description]"></td>
<td><input type="text" name="events[][type]">
<td><input type="text" name="events[][class]">
<td><input type="text" name="events[][issue_expert]"></td>
Now when you submit the form and it is parsed, params['events'] will contain an array of hashes, each with keys description, type, class and issue_expert. You can then iterate over this array and process the data as appropriate. You may want to add a hidden id input with each hash to make sure each set of data is associated with the correct record (it might look something like <input type=hidden name="events[][id]" value="<% event.id %>">).
So i'm trying to get from the database values to display. Currently, i can get any text values but i cannot get the checkboxes to work and i don't understand why. Using the following code
<td>Street: <input type="text" name="Address1" value="<% out.print(MyProperties.getString(3)); %>" disabled="true"></td>
<td>No. of Bedrooms: <input type="text" name="Bedrooms" value="<% out.print(MyProperties.getString(9)); %>" disabled="true"></td>
<td>Garden: <input type="checkbox" name="Garden" disabled="true" <% if(MyProperties.getString(10) == "1"){ out.print("checked='checked'");} %>></td>
This will draw out values as text for "address1" and "bedrooms" but i cannot get the checkbox for garden to work.
In the database, i have text values for the first two working variables, and the value for the checkbox is either text(string) or int (sorry, i can't remember exactly and i need to check it) but what i've written doesn't work for either getString(9) == "1" or getInt(9) == 1
However i can take the value from the database and print it out using the line
<td><% out.print(MyProperties.getString(10)); %></td>
does work.
Keep in mind that both of my tables are within a while loop
<% while (MyProperties.next()){ %> TABLE <% } %>
try this code
<%=MyProperties.getInt(10) == 1?"checked":""%>
Hope this helps !!
I am creating checkboxes at runtime, in a while loop according to an amount of data coming from a database.
Now I want to know what I should write in the attribute name to give a different name to each checkbox. Also for the value, if I want to pass the value of rs.getString(1) using jsp.
Here is a piece of my code.
<%while(rs.next())
{
%><tr><td align="center"><input type="checkbox" name="" value="">
<%
for(int i=1;i<=5;i++)
{
%>
<td align="center">
<%=rs.getString(i)%>
</td>
<%}%>
</tr>
<%}%>
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.