JSP affecting checbox variable using database - html

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 !!

Related

Show auto-increment ID from database into a html text-field with JSP

This question was asked earlier in 2016 but the guy that asked didn't get the answer. So does anyone know how to get this ID which is already AI but I need it shown automatically in a html form.
So I am inserting a new user which still doesn't have ID assigned .
<h3>Kreirati korisnika</h3>
<form action="kreiraj_korisnika.jsp">
<div class="form-group" >
<label for="id_clan" >ID:</label>
<input type="text" class="form-control" id="id_clan" name="id_clan">
</div>
First you have to get the next possible Id.
Check this for the instructions:
https://dev.mysql.com/doc/refman/8.0/en/getting-unique-id.html
Then you can work with the data:
The textbox:
<input type="text" class="form-control" id="id_clan" name="id_clan" value="<%= yourId%>"/>
JSP code:
<%! String yourId; %>
<%
//do some code to receive your data
yourId="YOUR_ID";
%>
See here for more info:
Assign value to HTML textbox from JSP
or
https://www.quora.com/How-do-I-pass-the-text-entered-into-an-HTML-text-box-to-a-Java-method-if-I-am-using-JSP

how to pass retrieved mysql data into a hidden input tag inside a form

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.

Save all user entries from html text built off a loop using ruby and sinatra

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 %>">).

Backbone.js hidden fields on submit

I am in the process of integrating my custom shopping cart with a payment gateway(Paypal).
I need to submit my items with the use of hidden fields, but when I submit the form the hidden fields seems to be missing. (According to the the payment gateway my cart is empty)
I know it might be a simple problem, but I've been going in circles for the last couple of hours.
The code that I generate the hidden fields with:
<% for(i=0,l=products.length;i<l;++i) { p = products[i]; %>
<input type="hidden" name= "item_name_"+<% i %> value="beach ball">
<input type="hidden" name="amount_1" value="15">
<% } %>
Any help would be appreciated.
Thanks
Yeah, it's quite simple :)
<input type="hidden" name="item_name_<%= i %>" value="beach ball" />
FYI:
<% i %> evaluate
<%= i %> interpolate
<%- i %> escape

How to Post Data from HTML Table in Classic ASP

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%> />