พอดีมีปัญหาขึ้นอะจ้า จะเขียนโปรแกรมให้มันแสดงผลข้อมูลแบบ Drop down list อะจ้า โดยที่กด list box หนึ่ง ก็จะเปลี่ยนข้อมูลในอีก list box หนึ่งจ้า
ตอนนี้เขียนให้มันเปลี่ยนแล้ว แต่มันไม่ขึ้นข้อมูลให้อะ โดยเขียนเป็นแบบ framework javascript จ้า ตัวอย่าง(บางส่วนจ้า)
ไฟล์ a.jsp
<%@page import="java.sql.*,java.sql.Statement,java.util.*,java.util.Date,java.text.*,EJP.ThaiUtilities "%>
<%@page contentType="text/html;charset=TIS-620"%>
<%@ include file="config/var.jsp" %>
<script type="text/javascript" src="myAjaxfmw.js"></script>
<form name="formEdit" id="formEdit" method="post" action="editcar.jsp" onSubmit="return chkEdit();" >
<br>
<input type="submit" name="edit" value="แก้ไขข้อมูล" />
<input type="reset" name="Reset" value="Reset" >
<tr>
<td align="center"> รหัสยี่ห้อ </td>
<td align="center">
<select name="car_brcode" id="car_brcode " onchange="loadXMLDoc()">
<option value="<%=result2.getString("car_brcode")%> "><%=result2.getString("car_brcode")%></option>
<% while (result.next()) {%>
<option value="<%=result.getString("car_brcode")%>"><%=result.getString("car_brcode")%>-<%=new String(result.getString("car_tbrand").getBytes("ISO8859_1"),"TIS-620")%></option>
<%}%>
</select>
</td>
</tr>
<tr>
<td align="center"> running code </td>
<td align="center">
<select name="md_code" id="md_code">
<%-- while (result4.next()) {--%>
<option>---- option ----</option>
<%-- } --%>
</select>
</td>
</tr>
ไฟล์ b.jsp
<%@ page import="java.io.*,java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/xml");
String brcode = request.getParameter("ok");
int i=Integer.parseInt(brcode);
Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:sybase:Tds:ams2srv.prachakij.com:7100/amsdata", "sa", "");
Statement Stmt= connection.createStatement();
// String sql = "select md_code,md_type,md_model,tbcarproperty.prop_code,car_property from tbcarmodel,tbcarproperty where tbcarproperty.prop_code = tbcarmodel.prop_code AND car_brcode = "+ brcode;
ResultSet rs = Stmt.executeQuery("select md_code,md_type,md_model,tbcarproperty.prop_code,car_property from tbcarmodel,tbcarproperty where tbcarproperty.prop_code = tbcarmodel.prop_code AND car_brcode = "+ i);
String aa = "";
String a = "";
out.println("<tbcarmodel>");
out.println("<tbcarproperty>");
while(rs.next())
{
aa = new String(rs.getString("md_type").getBytes("ISO8859_1"),"TIS-620");
a = new String(rs.getString("car_property").getBytes("ISO8859_1"),"TIS-620");
out.println("<md_code>"+rs.getString(1)+"</md_code>"+"-"+"<md_type>"+aa+"</md_type>"+"-"+"<md_model>"+rs.getString(3)+"</md_model>"+"-"+"<prop_code>"+rs.getString(4)+"</prop_code>"+"-"+"<car_property>"+a+"</car_property>");
}
out.println("</tbcarmodel>");
out.println("</tbcarproperty>");
//out.print(rs);
rs.close();
Stmt.close();
connection.close();
%>
ไฟล์ ajaxframework.js
function loadXMLDoc()
{
var xmlhttp;
var keys=document.formEdit.car_brcode.value
var urls="md_code.jsp?ok="+keys
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
removeall();
if (xmlhttp.readyState==4)
{
z=0;
var roott=xmlhttp.responseXML.documentElement;
var ress=roott.getElementsByTagName("md_code")[z].childNodes[0].nodeValue;
var a=roott.getElementsByTagName("md_type")[z].childNodes[0].nodeValue;
var aa=roott.getElementsByTagName("md_model")[z].childNodes[0].nodeValue;
var arg=roott.getElementsByTagName("prop_code")[z].childNodes[0].nodeValue;
var carp=roott.getElementsByTagName("car_property")[z].childNodes[0].nodeValue;
while(ress!=null)
{
z++
var ress=roott.getElementsByTagName("md_code")[z].childNodes[0].nodeValue;
var a=roott.getElementsByTagName("md_type")[z].childNodes[0].nodeValue;
var aa=roott.getElementsByTagName("md_model")[z].childNodes[0].nodeValue;
var arg=roott.getElementsByTagName("prop_code")[z].childNodes[0].nodeValue;
var carp=roott.getElementsByTagName("car_property")[z].childNodes[0].nodeValue;
addoptions(ress,a,aa,arg,carp)
}
}
function removeall()
{
var ct=document.formEdit.md_code.length;
for(i=ct; i>=0; i--) {
document.formEdit.md_code.options=null;
}
}
function addoptions(reslt,elen,del,rou,ry)
{
//alert(del);
var ct1=document.createElement("option");
ct1.text=reslt+"-"+elen+"-"+del+"-"+rou+"-"+ry;
ct1.value=reslt+"-"+elen+"-"+del+"-"+rou+"-"+ry;
document.formEdit.md_code.options.add(ct1);
}
}
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
สอบถามเรื่องการเขียนโปรแกรม Jsp และ Ajax XML จ้า
ตอนนี้เขียนให้มันเปลี่ยนแล้ว แต่มันไม่ขึ้นข้อมูลให้อะ โดยเขียนเป็นแบบ framework javascript จ้า ตัวอย่าง(บางส่วนจ้า)
ไฟล์ a.jsp
<%@page import="java.sql.*,java.sql.Statement,java.util.*,java.util.Date,java.text.*,EJP.ThaiUtilities "%>
<%@page contentType="text/html;charset=TIS-620"%>
<%@ include file="config/var.jsp" %>
<script type="text/javascript" src="myAjaxfmw.js"></script>
<form name="formEdit" id="formEdit" method="post" action="editcar.jsp" onSubmit="return chkEdit();" >
<br>
<input type="submit" name="edit" value="แก้ไขข้อมูล" />
<input type="reset" name="Reset" value="Reset" >
<tr>
<td align="center"> รหัสยี่ห้อ </td>
<td align="center">
<select name="car_brcode" id="car_brcode " onchange="loadXMLDoc()">
<option value="<%=result2.getString("car_brcode")%> "><%=result2.getString("car_brcode")%></option>
<% while (result.next()) {%>
<option value="<%=result.getString("car_brcode")%>"><%=result.getString("car_brcode")%>-<%=new String(result.getString("car_tbrand").getBytes("ISO8859_1"),"TIS-620")%></option>
<%}%>
</select>
</td>
</tr>
<tr>
<td align="center"> running code </td>
<td align="center">
<select name="md_code" id="md_code">
<%-- while (result4.next()) {--%>
<option>---- option ----</option>
<%-- } --%>
</select>
</td>
</tr>
ไฟล์ b.jsp
<%@ page import="java.io.*,java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/xml");
String brcode = request.getParameter("ok");
int i=Integer.parseInt(brcode);
Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:sybase:Tds:ams2srv.prachakij.com:7100/amsdata", "sa", "");
Statement Stmt= connection.createStatement();
// String sql = "select md_code,md_type,md_model,tbcarproperty.prop_code,car_property from tbcarmodel,tbcarproperty where tbcarproperty.prop_code = tbcarmodel.prop_code AND car_brcode = "+ brcode;
ResultSet rs = Stmt.executeQuery("select md_code,md_type,md_model,tbcarproperty.prop_code,car_property from tbcarmodel,tbcarproperty where tbcarproperty.prop_code = tbcarmodel.prop_code AND car_brcode = "+ i);
String aa = "";
String a = "";
out.println("<tbcarmodel>");
out.println("<tbcarproperty>");
while(rs.next())
{
aa = new String(rs.getString("md_type").getBytes("ISO8859_1"),"TIS-620");
a = new String(rs.getString("car_property").getBytes("ISO8859_1"),"TIS-620");
out.println("<md_code>"+rs.getString(1)+"</md_code>"+"-"+"<md_type>"+aa+"</md_type>"+"-"+"<md_model>"+rs.getString(3)+"</md_model>"+"-"+"<prop_code>"+rs.getString(4)+"</prop_code>"+"-"+"<car_property>"+a+"</car_property>");
}
out.println("</tbcarmodel>");
out.println("</tbcarproperty>");
//out.print(rs);
rs.close();
Stmt.close();
connection.close();
%>
ไฟล์ ajaxframework.js
function loadXMLDoc()
{
var xmlhttp;
var keys=document.formEdit.car_brcode.value
var urls="md_code.jsp?ok="+keys
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
removeall();
if (xmlhttp.readyState==4)
{
z=0;
var roott=xmlhttp.responseXML.documentElement;
var ress=roott.getElementsByTagName("md_code")[z].childNodes[0].nodeValue;
var a=roott.getElementsByTagName("md_type")[z].childNodes[0].nodeValue;
var aa=roott.getElementsByTagName("md_model")[z].childNodes[0].nodeValue;
var arg=roott.getElementsByTagName("prop_code")[z].childNodes[0].nodeValue;
var carp=roott.getElementsByTagName("car_property")[z].childNodes[0].nodeValue;
while(ress!=null)
{
z++
var ress=roott.getElementsByTagName("md_code")[z].childNodes[0].nodeValue;
var a=roott.getElementsByTagName("md_type")[z].childNodes[0].nodeValue;
var aa=roott.getElementsByTagName("md_model")[z].childNodes[0].nodeValue;
var arg=roott.getElementsByTagName("prop_code")[z].childNodes[0].nodeValue;
var carp=roott.getElementsByTagName("car_property")[z].childNodes[0].nodeValue;
addoptions(ress,a,aa,arg,carp)
}
}
function removeall()
{
var ct=document.formEdit.md_code.length;
for(i=ct; i>=0; i--) {
document.formEdit.md_code.options=null;
}
}
function addoptions(reslt,elen,del,rou,ry)
{
//alert(del);
var ct1=document.createElement("option");
ct1.text=reslt+"-"+elen+"-"+del+"-"+rou+"-"+ry;
ct1.value=reslt+"-"+elen+"-"+del+"-"+rou+"-"+ry;
document.formEdit.md_code.options.add(ct1);
}
}
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}