I currently have a MySQL database with a table 'description' containing a 'title' and 'contents' variables. What I'd like to do, is dynamically create buttons who's value attributes are the 'title' of each 'description' row. Then I'd like to have the buttons display the 'contents' value when their respective 'title' is clicked.
The problem is I'm not sure how to go about inserting a JSP String variable into a button 'value' attribute dynamically. Is there any way of doing this without javascript?
This is the code I have:
Getting the description objects:
<%
List<Description> descriptions = DescriptionDB.getDescriptions();
%>
Scriptlet for the table of buttons:
<table border="1" id="titleTable">
<%
if (descriptions != null) {
for (Description description : descriptions) {
String title = description.getDescriptionTitle();
%>
<tr>
<td><a id="bt" type="button" value="title"</td>
</tr>
<%
}
}
%>
</table>
I would like the value="title" to be the String title in the scriptlet.
It should be as easy as
%>
<tr>
<td><a id="bt" type="button" value="<%= title %>"</td>
</tr>
<%
Be aware though that the use of scriptlets seems to be considered sort of bad practice by some today.
Related
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')
})
}
I have numerous objects (customers) populating a table and I need to edit a particular field for each row/customer (via radio buttons) and save all via a single submit button at the bottom of the table.
I figure it would be best to render a form for each line ("accept_reject_form") in which I have my two radio buttons but I can't figure out how to have the form so on submission each selection is saved for each of the customers.
I've spent a while looking at a few similar questions, but none quite address this problem. Any help would be much appreciated :-)
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Available Actions</th>
</tr>
</thead>
<tbody>
<% #customers.each do |customer| %>
<tr>
<th><%= customer.name %></th>
<th><%= customer.age %></th>
<th><%= render 'accept_reject_form' %></th>
</tr>
<% end %>
</tbody>
</table>
You don't need a form per line: each form generates a request, this is hugely inefficient. Instead, the data submitted by the form needs to be structured in such a way that your controller action can update multiple records just by looking at the contents of params.
I would make sure that, in the accept_reject_form partial, you don't have the form tag and just have the fields. The form tag should be wrapped around the block where you iterate over #customer: in this case it will need to go around the whole table.
Give each field in your form partial a name value like
"customers[#{customer.id}][first_name]"
"customers[#{customer.id}][last_name]"
etc
This will then come through to the controller like
params = {:customers => {123 => {:first_name => "John", :last_name => "Smith"}, 456 => {:first_name => "Susan", :last_name => "Thompson"}}
Then in the controller (eg the update action) you can do something like this:
if params[:customer] && params[:id]
#traditional params structure for updating a single record
#customer = Customer.find_by_id(params[:id])
#customer.update_attributes(params[:customer])
elsif params[:customers] #note plural
#new params structure for updating multiple records
#customers = []
params[:customers].each do |id, attrs|
customer = Customer.find_by_id(id)
customer.update_attributes(attrs)
#customers << customer
end
end
this could use some error checking but you get the idea, hopefully.
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.
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
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