This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "ajax" Watch "ajax" New topic
Author

ajax

pooja sawant
Greenhorn

Joined: Jan 03, 2013
Posts: 2
hiii,
i have three dropdown list, in which the 1st drop down list populates data from database and depending upon the value selected in 1st dropdown the values in 2nd dropdown changes. Now wen i select any value in 2nd dropdown the values in 3rd dropdown should be populated accordingly. I m successfully able to populate values in the 2nd drop down using ajax but i m getting null value in the 3rd dropdown..i m attaching my code. please kindly post the answer as soon bcuz its urgent..
my main jsp:


<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
function showtype(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("domain_name").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("div1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","domain_name1.jsp?dept_name="+str,true);
xmlhttp.send();
}

function showtype1(str)
{
alert('hi')
var xmlhttp1;
if (str=="")
{
document.getElementById("control_desc").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp1=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp1.onreadystatechange=function()
{
if (xmlhttp1.readyState==4 && xmlhttp1.status==200)
{
document.getElementById("div2").innerHTML=xmlhttp1.responseText;
}
}
xmlhttp1.open("GET","control_desc1.jsp?domain_name="+str,true);
xmlhttp1.send();
}

</script>

</head>
<body style="font-family: Cambria; background-color:beige;">
<font size="4">Select </font>
<p><font color="red"><sup> * </sup></font><i>Indicates that the field is mandatory.</i></p>
<form action="Screate_question" name="form">
<br>
<img src='images/horibar.png'/>
<br>
<table>
<tr>
<td>
Type of Organization: </td><td><select id="org_industry" name="org_industry" style="border-color: black;">
<option value="">Select Organization type</option>
<%
Connection conn=null;
ResultSet rs=null;
String DriverName="oracle.jdbc.OracleDriver";
Class.forName(DriverName);
String url ="jdbcracle:thin:@localhost:1521:XE";
conn=DriverManager.getConnection(url,"SYSTEM","SYSTEM");
String q=null;
PreparedStatement ps=null;
q="select org_industry from organisation_details";
ps=conn.prepareStatement(q);
rs=ps.executeQuery();
while(rs.next())
{%><option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>

<% }
%>
</select>
</td>
<td>
Department Name: </td><td><select id="dept_name" name="dept_name" onchange="showtype(this.value);showtype1(this.value);" style="border-color: black;">
<option value="">Select Department</option>
<%
// Connection conn1=null;
ResultSet rs1=null;
String q1=null;
PreparedStatement ps1=null;
q1="select dept_name from department";
ps1=conn.prepareStatement(q1);
rs=ps1.executeQuery();
System.out.println();
while(rs.next())
{%><option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>

<% }
%>
</select>
</td>
</tr>
<tr>
<td>
Domains </td><td>
<div id="div1">
<select id="domain_name" name="domain_name" onchange="selected();" style="border-color: black;">
<option value="">Select domain</option>
</select>
</div>
</tr>
<tr>
<td>Sub Domain :</td>
<td><div id="div2">
<select name="control_desc" id="control_desc" style="border-color: black;">
<option value="none"> Select Sub Domain </option>
</select>
</div>
</td>
</tr>
</table>
<br>

<img src='images/horibar.png'/>


<br>
<input type="submit" value="Next" name="button" style="border-color: black; background-color:burlywood ; color:black;" />

<br>
</form>
</body>
</html>




my domain_name.jsp:
<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%

String Dept_name=request.getParameter("dept_name");
System.out.println(Dept_name);
String buffer="<select id='domain_name' name='domain_name' style='border-color: black;'>";
try
{
Connection conn=null;
ResultSet rs=null;
String DriverName="oracle.jdbc.OracleDriver";
Class.forName(DriverName);
String url ="jdbcracle:thin:@localhost:1521:XE";
conn=DriverManager.getConnection(url,"SYSTEM","SYSTEM");
String q=null;
PreparedStatement ps=null;
q="select dd_name from dept_domain dd where dd.dept_id=(select dept_id from department de where de.dept_id=dd.dept_id and de.dept_name='"+Dept_name+"')";
System.out.println("aftr query");
ps=conn.prepareStatement(q);
rs=ps.executeQuery();
System.out.println("before while");
while(rs.next())
{System.out.println("rs start");
buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(1)+"</option>";
}
buffer=buffer+"</select>";
System.out.println(buffer);
PrintWriter sout = response.getWriter();
sout.println(buffer);
System.out.println("1");

String Domain_name=request.getParameter("domain_name");
System.out.println(Domain_name);
String buffer1="<select name='control_desc' id='control_desc' style='border-color: black;'>";

ResultSet rs1=null;
String q1=null;
PreparedStatement ps1=null;
q1="SELECT CONTROL_DESC FROM SUB_DOMAIN SD WHERE SD.DOMAIN_ID=(SELECT DOMAIN_ID FROM DOMAIN D WHERE SD.DOMAIN_ID=D.DOMAIN_ID AND D.DOMAIN_NAME='"+Domain_name+"')";
System.out.println("aftr query");
ps=conn.prepareStatement(q1);
rs=ps1.executeQuery();
System.out.println("before while");
while(rs.next())
{System.out.println("rs start");
buffer1=buffer1+"<option value='"+rs.getString(1)+"'>"+rs.getString(1)+"</option>";
}
buffer1=buffer1+"</select>";
System.out.println(buffer);
PrintWriter sout1 = response.getWriter();
sout1.println(buffer1);
System.out.println("2");
}
catch(Exception e)
{}

%>


control_desc.jsp:
<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<script>
alert("hiiiiii");
function selected()
{
var e = document.getElementById("domain_name");
var strUser = e.options[e.selectedIndex].value;
alert(strUser);
}

</script>
</head>
<body>

<%

String Domain_name=request.getParameter("domain_name");
System.out.println(Domain_name);
String buffer="<select name='control_desc' id='control_desc' style='border-color: black;'>";
try{
Connection conn=null;
ResultSet rs=null;
String DriverName="oracle.jdbc.OracleDriver";
Class.forName(DriverName);
String url ="jdbcracle:thin:@localhost:1521:XE";
conn=DriverManager.getConnection(url,"SYSTEM","SYSTEM");
String q=null;
PreparedStatement ps=null;
q="SELECT CONTROL_DESC FROM SUB_DOMAIN SD WHERE SD.DOMAIN_ID=(SELECT DOMAIN_ID FROM DOMAIN D WHERE SD.DOMAIN_ID=D.DOMAIN_ID AND D.DOMAIN_NAME='"+Domain_name+"')";
System.out.println("aftr query");
ps=conn.prepareStatement(q);
rs=ps.executeQuery();
System.out.println("before while");
while(rs.next())
{System.out.println("rs start");
buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(1)+"</option>";
}
buffer=buffer+"</select>";
System.out.println(buffer);
PrintWriter sout = response.getWriter();
sout.println(buffer);
System.out.println("2");
}
catch(Exception e)
{}

%>
</body>
</html>

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ajax
 
Similar Threads
javascript pagination
jsp + html: only this jsp javascript code is not working
request.getParameter return null value
how to display image with data in jsp page
How to retain selected value from dropdown list in same jsp page