I have the below code that display list of values from database in drop down. The query also selects Jt_JOB_Description. I would like to display the Jt_JOB_Description based on the JT_JOB_TITLE selected in Text Area. Help plz.
<select name="jTitle" id="jTitle" style="background-color: #D8D8D8">
<%
Statement stt = conn.createStatement();
ResultSet rstt = stt.executeQuery("SELECT JT_JOB_TITLE, JT_JOB_DESCRIPTION FROM uap.dbo.UAP_JOB_TITLE ORDER BY JT_JOB_TITLE");
while (rstt.next()) {
%>
<option value="<%=rstt.getString("JT_JOB_TITLE")%>"> <%=rstt.getString("JT_JOB_TITLE")%>
</option>
<%
}
stt.close();
rstt.close();
%>
</select>
<textarea class="styled" rows="5" cols="12" name="jDesc" id="jDesc" ></textarea>
Modify your code as given below
<select name="jTitle" id="jTitle" style="background-color: #D8D8D8" onchange="setText(this)">
<%
String str="";
Statement stt = conn.createStatement();
ResultSet rstt = stt.executeQuery("SELECT JT_JOB_TITLE, JT_JOB_DESCRIPTION FROM uap.dbo.UAP_JOB_TITLE ORDER BY JT_JOB_TITLE");
while (rstt.next()) {
str+=rstt.getString(1)+"#"+rstt.getString(2)+"#";
%>
<option value="<%=rstt.getString("JT_JOB_TITLE")%>"><%=rstt.getString("JT_JOB_TITLE")%>
</option>
<%
}
stt.close();
rstt.close();
%>
</select>
<input type="hidden" name="txtHidStr" id="txtHidStr" value="<%=str %>" />
<textarea class="styled" rows="5" cols="12" name="jDesc" id="jDesc" ></textarea>
and wirte following javascript function
function setText(element){
var arr_main=document.getElementById("txtHidStr").value.split("#");
for(i=0;i<arr_main.length;i++)
{
arr_val=arr_main[i].split("#");
if(arr_val[0]==element.value)
{
document.getElementById("jDesc").innerHTML=arr_val[1];
break;
}
}
}
Related
I have created a web project that need to register user data in database table that can be used further.
registration.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Registration</title>
<link rel="stylesheet" href="style.css">
<script language="JavaScript" type="text/javascript">
function validateform(theForm) {
var First_Name = theForm.First_Name;
var Last_Name = theForm.Last_Name;
var UserName = theForm.UserName;
var phoneNo = theForm.phoneNo;
var password = theForm.password;
var reenter_password = theForm.reenter_password;
var email = theForm.email;
var gender = theForm.gender;
var title = theForm.title;
var day = theForm.dobday;
var month = theForm.dobmonth;
var year = theForm.dobyear;
var atposition = email.value.indexOf("#");
var dotposition = email.value.lastIndexOf(".");
if (First_Name.value == "") {
alert("Name can't be empty");
First_Name.focus();
return false;
}
if (!First_Name.value.match(/^[A-Za-z]+$/)) {
alert("name contains invalid Characters!");
First_Name.focus();
return false;
}
if (Last_Name.value == "") {
alert("Name can't be empty");
Last_Name.focus();
return false;
}
if (!Last_Name.value.match(/^[A-Za-z]+$/)) {
alert("name contains invalid Characters!");
Last_Name.focus();
return false;
}
/* user name validation */
if (UserName.value == "") {
alert("user Name can't be empty");
UserName.focus();
return false;
}
/* email validation */
if (email.value == "") {
alert("please enter your email");
email.focus();
return false;
}
if (atposition < 1 || dotposition < atposition + 2 || dotposition + 2 >= email.value.length) {
alert("Please enter a valid e-mail address \n atpostion:" + atposition + "\n dotposition:" + dotposition);
email.focus();
return false;
}
/* password validation */
if (password.value == "") {
alert("password can't be empty");
password.focus();
return false;
}
if (password.value.length < 6) {
alert("Password must be at least 6 characters long.");
password.focus();
return false;
}
if (reenter_password.value == "") {
alert("please re-enter the password");
reenter_password.focus();
return false;
}
if (!password.value.match(reenter_password.value)) {
alert("password must be same!");
return false;
}
if (!gender[0].checked && !gender[1].checked) {
alert("please select your gender");
gender.focus();
return false;
}
return true;
}
</script>
</head>
<body id="html">
<center>
<br><br>
<section class="registration_form" >
<form name="registration" method="post" action="registerUser.jsp"
onsubmit="return validateform(this);">
<table id="tab1" bgcolor="#ffffff" width="60%" height="320%">
<td width="10%"></td><td><h1>Register here</h1></td>
<tr><td width="20%"></td><td><label for="First Name">First Name</label>
<input type="First Name" id="usernametxtbox" name="First_Name" placeholder="enter First Name" >
</td></tr>
<tr><td width="20%"></td><td><label for="Last Name">Last Name</label>
<input type="Last Name" id="usernametxtbox" name="Last_Name" placeholder="enter Last Name" >
</td>
</tr>
<tr><td width="20%"></td><td>
<label for="User Name">User Name</label>
<input type="User Name" id="txtbox" name="UserName" placeholder="enter User Name" ></td>
</tr>
<tr><td width="20%"></td>
<td><label for="usermail">Email</label>
<input type="email" id="txtbox" name="email" placeholder="yourname#email.com" ></td></tr>
<tr><td width="20%"></td><td>
<label for="password">Password</label>
<input type="password" id="txtbox" name="password" placeholder="password" ></td></tr>
<tr><td width="20%"></td><td><label for="reenter_password">re-enter Password</label>
<input type="password" id="txtbox" name="reenter_password" placeholder="Re-enter password"></td></tr>
<tr><td width="20%"></td><tr height="10%"><td></td></tr>
<tr><td width="20%"></td><td><label>Date of Birth:</label>
<select name=birthday>
<option> - Month - </option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="DOBDay">
<option> - Day - </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="DOBYear">
<option> - Year - </option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="196\6">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
</select></td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td><td><label>Gender:</label><input type="radio" name="gender" value="male" checked > Male
<input type="radio" name="gender" value="female"> Female</td>
</tr>
<tr><td width="20%"></td><td><input type="submit" value="register" id="butn">
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</form>
</section>
</center>
</body>
</html>
After submitted goes to registerUser.jsp
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.Connection"%>
<%# page import="java.io.*" %>
<html>
<body>
<%
String firstname=request.getParameter("First_Name");
String lastname=request.getParameter("Last_Name");
String username=request.getParameter("UserName");
String email=request.getParameter("email");
String password=request.getParameter("password");
String month=request.getParameter("birthday");
String day=request.getParameter("DOBday");
String year=request.getParameter("DOBYear");
String gender=request.getParameter("gender");
String DOB=day+"-"+month+"-"+year;
String connectionURL = "jdbc:mysql://localhost:3306/demo?useSSL=false";
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "admin", "admin");
statement = connection.createStatement();
String QueryString = "insert into user_details(FIRST_NAME,LAST_NAME,USERNAME,EMAIL,PASSWORD,DOB,GENDER) values( '"+firstname+"',' " +
lastname+"', '"+username+"', '"+email+"', '"+password+"',' "+DOB+","+gender+");";
statement.executeUpdate(QueryString);
%>
User data inserted successfully
<% }
catch (Exception ex) {ex.printStackTrace();}
finally{
statement.close();
connection.close();
}
%>
</body>
Every thing seems to be fine with variable names,checked if incorrect. Included MYSql connector. Server works perfect.
I doubt in unidentified datatype mismatch in db or any other server-page error, as it isn't coming up in stacktrace, it's hard to find the flaw.
Please help out on this !
I have table structure in MYSQL
I think you had made a mistake in the SQL query
String QueryString = "insert into user_details"+
"(FIRST_NAME,LAST_NAME,USERNAME,EMAIL,PASSWORD,DOB,GENDER) values( '"+
firstname+"',' " + lastname+"', '"+
username+"', '"+email+"', '"+password+"','"+DOB+"','"+gender+"');";
if still not work then change this following code
check this code if work this work for me:
Connection connection;
Statement statement;
String driver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost:3306/demoDb";
String login = "root";
String password = "";
try {
Class.forName(driver);
connection = DriverManager.getConnection(dbURL, login,password);
statement = connection.createStatement();
} catch (SQLException e) {
connection = null;
System.out.println("drivernot found");
System.out.println("SQLException:" + e);
} catch (Exception e) {
System.out.println("Exception:" + e);
}
I have below code in my jsp :
<select name="productId" id ="productId" onchange="getDetail()">
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
On page refresh(F5), the values in the drop down is not getting refreshed.The previously selected value remain there. DB call to fetch the detail is in JSP itself.
How to refresh the values?
Try JSTL <c:forEach> Tag instead
<select name="productId" id ="productId" onchange="getDetail()">
<c:forEach var="item" items=${yourListNameStoredInSomeScope}>
<option value="{item}">${item}<option>
</c:forEach>
</select>
try something like this:
<select name="productId" id ="productId" onchange="getDetail()">
<option value=" " selected></option>
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
if you use selected attribute , when the drop down is loaded, the pre-selected option is the default in your case " ".
so i'm trying to pass selected value from a combobox in a jsp to another jsp here is what i got myself into
<form name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>
<button onclick="window.location.href='/acceuil.jsp?mat=' JSGetSelectedItemMat() " >Statistique</button>
<script>
function JSGetSelectedItemMat()
{
var e = document.getElementById("soflow");
var strSel = e.options[e.selectedIndex].value;
document.getElementById("btt").value = strSel;
}
</script>
try this code to get selected valued from select tag. As there is no need to use js, you are using post method in your html form. Also get the value in acceuil.jsp using getParameter method. that is request.getParameter("mat");
<form action="acceuil.jsp" name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>
I'm tying to bind the value to dropdownlist i.e named as subtype which is depending on another dropdownlist i.e named as type. Please tell me how I can do that.. I have given I following code which implement for binding the dropdownlist depending on another dropdownlist.
`<table>
<tr>
<td>
<label id="type">Chosen category : </label>
</td>
<td>
<% String getType=request.getParameter("id");
out.println("<h4>"+getType+" <a href='post free ads.jsp'>Change the
category</a> </h4>");
%>
</td>
</tr>
<tr>
<td>
<label id="typeselect">Select type : </label>
</td>
<td>
<select name="type">
<option value="none">Select</option>
<%
Connection conn = NewClass.getOracleConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select distinct company from admin.all_stuff
where stuff_type='"+getType+"' ");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
<%
}
%>
</select>
</td>
</tr>
<tr>
<td> <label id="subtypeselect">Select Subtype : </label></td>
<td>
<select name="subtype">
<option value="none">Select</option>
<%
try
{ %>
<% String typeselected = request.getParameter("type");
Statement stmtmt = conn.createStatement();
ResultSet rse = stmtmt.executeQuery("Select model_no from admin.all_stuff
where stuff_type='"+getType+"' and company='"+typeselected+"' ");
while(rse.next()){
%>
<option value="<%=rse.getString(1)%>"><%=rse.getString(1)%></option>
<%
}
%>
<% }
catch(Exception e)
{
out.println(e.getMessage());
}
%>
}
</select>
</td>
</tr>
</table>`
You need AJAX for this to get it work.
Your jsp is compiled into a servlet and then delivered to your client. After that no more changes are possible.
You have to handle an onChange event on the first drop down box in HTML and then post an AJAX request to your server to send back the content for the second drop down box.
Easiest way to start with AJAX is to get some library for example jQuery
$.get('ajax/test.html', function(data) {
alert(data);
});
This code allow us to download from HTML page some content from URL 'ajax/test.html'.
Second argument is callback function. Variable data has content of page 'ajax/test.html'
More: http://api.jquery.com/jQuery.get/
$('form#injuryReportSubmit').submit(function(){
if(($('form#injuryReportSubmit textarea.description').val().length <=0) || ($('form#injuryReportSubmit select.part').val()== "choose one...") ||($('form#injuryReportSubmit select.type').val() == "choose one...") || ($('form#injuryReportSubmit select.weather').val()=="choose one..."));
{
$('div#errorMessage').show();
return false;
}
});
The code above is used to validate a form before it submits. The problem is the form will not submit even when all the test are false. Can someone help?
the form is on a jsp and looks like
<form id ="injuryReportSubmit" method ="post" action="injuryReportingPage.html" >
<p class ="first" >Date Of Injury</p>
<p class ="second">Date Reported to Manager</p>
<input type="text" id="dateOfInjury" name="dateOfInjury">
<input type="text" id="dateReported" name ="dateReported">
<p class ="weather">Weather</p>
<p class ="injuryType">Injury Type</p>
<p class ="bodyPart">Body Part</p>
<p class ="time">Time Injury Occurred</p>
<p class ="description">Description</p>
<select class ="weather" name="weather">
<%if(InjuryReportController.getWeatherList() != null){ %>
<% for(Weather weather : InjuryReportController.getWeatherList()){%>
<option><%= weather.getWeatherCondition() %></option>
<%} }%>
<option >choose one...</option>
</select>
<select class ="type" name="injuryType">
<%if(InjuryReportController.getInjuryTypeList() != null){ %>
<% for(InjuryType injuryType : InjuryReportController.getInjuryTypeList()){%>
<option><%= injuryType.getInjuryTypeName() %></option>
<%} }%>
<option>choose one...</option>
</select>
<select class ="part" name="bodyPart">
<%if(InjuryReportController.getBodyPartList() != null){ %>
<% for(BodyPart bodyPart : InjuryReportController.getBodyPartList()){%>
<option><%= bodyPart.getBodyPartName() %></option>
<%} }%>
<option >choose one...</option>
</select>
<input type="text" id="timeP" name ="timeOfInjury" value="01:00 AM">
<textarea class ="description" rows="120" cols="670" name="description"></textarea>
<input id ="report" value="Submit Report" type ="submit">
</form>
Remove the semicolon from the end of line 5 of your code.
Currently what you've got is like this:
if (/*your conditions*/); // <- note the semicolon
{
...
return false;
}
This means that the block with the curly braces is not associated with the if statement and so will execute every time. Obviously then returning false every time cancels every submit.