Pass dynamic checkbox values to next JSP page - mysql

I have aproblem. Here I am showing some bills from Invoice table in mysql. And for single bill I am keeping a checkbox. I want to pass selected checkbox values to next jsp page. Please help me to do it. Thanks in advance.
<%
String company_name=request.getParameter("c_name");
try
{
Integer count=0;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_fleet","root","aadi");
Statement st = con.createStatement();
String sql="select invoice_no, invoice_date, gross_amount from tbl_invoice where client='"+company_name+"'";
ResultSet rs=st.executeQuery(sql);
%>
<center>
<table id="show_table">
<tr>
<td>PNR No</td>
<th>Date</td>
<th>Amount</td>
<th>Select</td>
</tr>
<%
while(rs.next())
{
%>
<tr>
<td><%=rs.getInt(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getInt(3)%></td>
<td><input type="checkbox" name="ck"/></td>
</tr>
%>
</table>
</center>
<%
}
catch(Exception e)
{
out.println(e);
}
%>

Hey I found the solution. On action page where I am going to receive values I will store all checkbox names in a array of string.
String selected_Checkboxes=request.getParameterValues("ck");
I will just change "td" tag of checkbox. I will put value of PNR NO which is coming from database and will put in checkbox.
<td><input type="checkbox" value="<%=rs.getInt(1)%>" name="ck"/></td>
I will use indexes of "selected_Checkboxes" to do further coding.
--Thanks Santino 'Sonny' Coleone

Related

get column data and insert into a row with id given in another column - sql nodejs

I have made a certain form with a table layout
the table is kind of like this below:
UAN NAME DESIGNATION No. of Days Present
87982 JOHN SALESMAN 25
now, the columns uan name and designation are already given (taken from the database) and the user only needs to put in the attendance.
the number of days will then be inserted into the database
in the row where UAN = 87892
The problem is that I am unable to figure out how to get the value of UAN from the page and use it to enter the number of days in the database.
could you give some idea how to accomplish this using nodejs??
I am using the ejs templating system and mysql database.
that's not the only problem.
I also want to enter the number of days present for all the different employees
where the number of days gets added to the row with their UAN number.
how am I supposed to do that?
any ideas??
here's the code for the attendance page
<!DOCTYPE HTML>
<html>
<head><title>attendance for employees</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>
<table>
<tr>
<th>Serial No.</th>
<th>UAN</th>
<th>Name</th>
<th>Designation</th>
<th>Attendance</th>
</tr>
<form action="/attendance-data" method="post">
<% var i=0 %>
<% rows.forEach(function(item){ %>
<% i=i+1 %>
<tr>
<td><%= i+"." %></td>
<td id="uan<%= i %>"> <%= item.uan %> </td>
<td><%= item.name %></td>
<td><%= item.designation %></td>
<td><input type="text" name="attendance <%= i %>" ></td>
</tr>
<% }); %>
</form>
</table>
</div>
<div>
<input type="button" value="submit" />
</div>
</body>
</html>
In order to get the data from the page and insert it into your database:
WHERE uan = '[data_from_html]', you will need to reference it by name. This can be accomplished when the user enters into the form how many days of attendance. The biggest issue you will have is that forms are tough to get working in correspondence to a table. You can put an entire table inside of a form but not the other way around necessarily.
Inside of your last td tag it would be best to place the entire form there with only one field visible and the rest hidden(you can still display as you were, just leave the other fields out of the form:
<td>
<form action='/attendance-data' method='post'>
<input type='hidden' id='uan<%= i %>' name='attendance' value ='<%= i %>'/>
<input type='hidden' name='item_name' value ='<%= i %>'/>
<input type='hidden' name='designation' value ='<%= item.designation %>'/>
<input type='text' name='attendance' value ='<%= i %>'/>
<input type="submit" value="Submit" />
</form>
</td>
Then inside of your routes in express:
app.post('/attendance-data', function(req, res) {
var uan = req.body.uan
var item_name = req.body.item_name
var designation = req.body.designation
var attendance = req.body.attendance
// Make sure they all contain some sort of value
var connString = new ConnectionString(
config.mysql.host,
config.mysql.port,
config.mysql.username,
config.mysql.password,
config.mysql.default_db
)
var MySqlConn = new MySqlConnection(connString)
// Proceed to do an update/insert into your db
MySqlConn.Connection.query("insert into [your_table] " +
"values(uan, item_name, designation, attendance) where uan='" + uan + "'",
function (err)
{
if (err)
{
console.log(err.stack)
throw err
}
// Otherwise success
res.status(200).send('blah')
})
}

How to pass parameter to Java using HTML

I am new to Java and programming in general. For background, I am working on my simple survey system. I have all the classes/methods necessary to insert and pull from MySQL and display the survey on my JSP page.
This is my Index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Survey System</title>
</head>
<body>
<h1>Survey List</h1>
<%
ResultSet resultset = new controller.SelectSurvey().SelectSurvey();
if (request.getParameter("btnControlSurvey") != null){
response.sendRedirect("controlsurvey.jsp");
}
if (request.getParameter("btnTakeSurvey") != null){
response.sendRedirect("takesurvey.jsp");
}
%>
<form name = 'surveylist' action="index.jsp" method="POST">
<table border="0">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select id="listsurvey">
<% while (resultset.next()) {
%>
<option><%= resultset.getString("survey_ID")%> - <%= resultset.getString("title")%></option>
<% } %>
</select>
</td>
<td>
<input type="submit" value="TAKE SURVEY" name = "btnTakeSurvey" />
</td>
</tr>
</tbody>
<td>
<input type="submit" value="CONTROL SURVEY" name = "btnControlSurvey" />
</td>
</table>
</form>
</body>
How do I pass the selected surveyID value in my drop down list with ID listsurvey to my hard-coded surveyID1 variable in my Takesurvey.jsp (see below) once the Submit button is clicked?
<%
int surveyID1 = 1;
ResultSet rsSurvey = new controller.SelectSurvey().SelectSurveyByID(surveyID1);
ResultSet rsSurveyQuestion = new controller.SelectSurvey().SelectQuestionByID(surveyID1);
%>
I found that there are at least three ways to do this as listed below. What would be the easiest way and would you please give me an example?
Putting values into the session object.
Putting values into the application object
Putting values at the end of the redirect URL.
Your inputs are very appreciated.
Thank you and have a good day.
Not sure what you're doing in that 2nd code segment, but you can get a hold of the form parameters using
String surveyId = request.getParameter(listsurvey);
It looks like you're looking for an int on the other side, but it comes in as a String so just use Integer.parseInt() after you pull it from the request.
Note: The request variable is already implicitly available in your JSP. You don't have to declare it.
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29
You need to pass the surveyID in the HTTP page request using either GET or POST. Then in the servlet / JSP you can use request.getParameter("surveyID")
I figured it out by using request.getParameters and use different forms for each button and it works as wanted.
Thank you for all of yours input again guys.

Best way to check jQuery POST method parameters for reducing jsp prepared statement query extra function

How can I make Submit TRUE result I mean from 2 INPUT field how to POST only one field of value to same JSP page like
<% string param=req.......
The most important thing is my PreparedStatement of jdbc have to be written twice for reason of two fields ... for checking Oracle DB of EMPLOYEE NO || NAME
By using Name field it's working fine and after changing of code by using Number it's working fine but I want you to help me is there any good way to code this stuff
this is HTML Form
<form id="contact" method="get">
<b>Employee No: </b> <input type="text" id="contact_number" size="15" name="number"/>
<b>Employee Name:</b> <input type="text" id="contact_name" size="25" name="name"/>
<input type="submit"/>
</form>
This is validation jQuery
$(document).ready(function() {
$("#contact").submit(function() {
if (($('#contact_number').val().length !== 0) && ($('#contact_name').val().length !== 0)) {
return false;
} else if (($('#contact_number').val().length === 0) && ($('#contact_name').val().length === 0)) {
return false;
} else {
return true;
}
});
});
This is complete JSP & String sql="Oracle query" & JDBC
<%String number = request.getParameter("number");
String name = request.getParameter("name");
if (name != null) { %>
<h3 id="myDiv"> Search results for <i> <%= name %> </i> </h3>
<% }
else { %>
<h4> Enter any Information on above field ... </h4>
<% } %>
<br>
<%#page import="java.sql.*" %>
<%Class.forName("oracle.jdbc.driver.OracleDriver");
String sql="SELECT * FROM RWEMP WHERE ENAME= ?";
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","hari","root");
PreparedStatement stat=con.prepareStatement(sql);
stat.setString(1,name);
ResultSet rs=stat.executeQuery();
try {
if(rs!=null) {
%>
<table class="gridtable" border=1 cellspan=60 cellpadding=16>
<tr>
<th> Emp ID </th>
<th> Emp Name </th>
<th> Emp Dept </th>
<th> Emp D.o.B </th>
<th> Show Record </th>
<th> Record Update </th>
<th> Compassionate </th>
</tr>
<%
while(rs.next()) {
%>
<tr>
<td><%= rs.getString("EID")%> </td>
<td><%= rs.getString("ENAME") %> </td>
<td><%= rs.getString("EDEPT")%> </td>
<td><%= rs.getString("EDOB")%> </td>
<td><input class="ui-button" type="button" onSubmit="result.jsp" target="destination" value="Show"></td>
<td><input class="ui-button" type="button" onclick="update.jsp" target="destination" value="update"></td>
<td><input class="ui-button" type="button" onclick="compassionate.jsp" target="destination" value="Compassionate"></td>
</tr>
<%
}
}
}
catch(SQLException e) {
e.printStackTrace();
}
con.close();
%>
</table>
For more Viewing Look at this Fiddle's HTML output only
How can i make Submit TRUE result ... i mean from 2 INPUT field .. how
to POST only one Field of Value to same JSP page ... like :- <% string
param=req.......
To do that put a dropdown which say select the criteria of search in your case is number and name and then provide him with a input field so when you submit form you have both columname and data to the next page and you dont have write 2 queries.Form should be like this
If possible help me that if user Click on Particular userS table row
... how to SEND that user row details to others jsp page ... for
complete record viewing or record editing purpose
Solution :Pass id of that row to other page
Create a column on each row name say Detail and do
<a href="/detailed.jsp?user_id="<%=user_id%>>Detail</a>
On detailed.jsp get value of user_id from get parameter and the apply the query with user_id to get all the details of that row

passing data from jsp to servlet from html table

I am developing a web application using JSP and Servlets.
In that application I have to show data from database table stud(studID, name, add) in html table, And each row in the table will have a hyperlink associated with it at the last column. After clicking on that hyperlink I wants to get the (studID) from the table...
so far I have done getting the data from database and then putting it into the column and then adding hyperlink for each row.. But I am not able to get the (studID) from the html table associated with hyperlink..
Thanks in advance....
Source code :
<%
String[][] data = (String[][])request.getAttribute("data");
String[] cNames = (String[])request.getAttribute("columnNames");
//headings
%>
<table>
<tr>
<%
for(int i=0;i<cNames.length;i++) {
%>
<th>
<%= cNames[i] %>
</th>
<%
}
//data if(data!=null)
for(int i=0;i<data.length;i++) {
%>
<tr>
<%
for(int a=0;a<3;a++) {
%>
<td>
<%=
data[i][a]
%>
</td>
<%
//hyperlink
if(a==2) {
%>
<td>
<a href="PlanProtocol" id=<%=i%> onclick="<% session.setAttribute("ID","p2"); %>" >Edit</a></td>
<%
}
}
%>
</tr>
<% } %>
<tr>
</table>
You can pass the id as a query string in the url. Simply:
My Link
Will work. But if you are using JSTL or another tag library then you can do something like this:
<c:url value="/myservlet" var="myURL">
<c:param name="id" value="1234"/>
</c:url>
mylink
And this has its advantages such as url encoding etc.
So to add the id to the URL in your posted code you can:
<a href="PlanProtocol?id=<%=i%>" >Edit</a>
And the url will end up like this: PlanProtocol?id=1234.
In the Servlet you can get the Parameter by:
request.getParameter("i");
However, as I mentioned above, you probably want to use a tag library like the JSTL rather than placing these scriptlets in your page. There are several advantages.
think you should pull out the studID in JSP and format the studID into the query string of the URL, html page. (?studID=xxxxx) So the servlet will know the studID.
You can use request.setAttribute("studID","value"); in your jsp page to set the value and use request.getAttribute("studID"); in servlet to get value

Assign value to HTML textbox from JSP

Hello I am creating a web page to add some information about given product.I need to enter id,name,description and image as information.I need the id to be auto generated.I am using jsp and database as access.I am fetching the count(*)+1 value from database and assigning to my html text box but its showing as null.can i get some help?
Code:
<body>
<%#page import="java.sql.*"%>
<%! String no; %>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:pd");
ResultSet rs = null;
Statement st = con.createStatement();
String sql = ("select count(*)+1 from products");
st.executeUpdate(sql);
while (rs.next()) {
no=rs.getString("count(*)+1");
}
rs.close();
st.close();
con.close();
}
catch(Exception e){}
%>
<Form name='Form1' action="productcode.jsp" method="post">
<table width="1024" border="0">
<tr>
<td width="10"> </td>
<td width="126">Add Product: </td>
<td width="277"> </td>
<td width="583"> </td>
</tr>
<tr>
<td> </td>
<td>Product Id:</td>
<td><label>
<input type="text" name="id" value="<%= no%>"/>
</label></td>
<td> </td>
.... and so on
{..}.executeQuery("Select max(id) from tablename");
This will return the ID with the largest number. Its more efficient than select * from tablename order by id desc limit 1.
However, it looks like your trying to guess/generate an ID of an object which doesn't yet exist in the database. This isn't the best way, and you may find that the ID your generating may be different to that generated by your DB. It could also cause duplication errors if two people are trying to create two new objects at the same time.
I would suggest that you don't provide a product id until the "create product" button has been pressed. Use ##IDENTITY; in your SQL command to set the new ID to your product safely.
You wrote a query to get the data like this
String sql = ("select count(*)+1 from products");
st.executeUpdate(sql);
The statement
executeUpdate()
only used to do the operations like INSERT,UPDATE
try
rs=st.executeQuery(sql);
instead of executeUpdate()