2016-11-26 47 views
1

如何在ajax(JSP)中傳遞選定的下拉值?

ñ我的web應用程序我使用的是動態下拉菜單選擇 類,分裂和student_id數據。當選擇課程 分區下拉填充根據選擇的類。類似地在 student_id下拉式填充通過獲得班級和部門。

我的問題是,我不能把類值,並在同一時間到JSP頁面的分度值..

我回到Home.jsp。代碼是..

<%@page import="java.sql.*"%> 
<html> 
     <head> 
     <script language="javascript" type="text/javascript"> 
     var xmlHttp 
     var xmlHttp 


     function showdivision(str){ 
     if (typeof XMLHttpRequest != "undefined"){ 
     xmlHttp= new XMLHttpRequest(); 
     } 
     else if (window.ActiveXObject){ 
     xmlHttp= new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     if (xmlHttp==null){ 
     alert("Browser does not support XMLHTTP Request") 
     return; 
     } 
     var url="divisionn.jsp"; 
     url +="?passclassname=" +str; 
     xmlHttp.onreadystatechange = divisionChange; 
     xmlHttp.open("GET", url, true); 
     xmlHttp.send(null); 
     } 




     function divisionChange(){ 
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
     document.getElementById("division").innerHTML=xmlHttp.responseText 
     } 
     } 



     function showstudid(str){ 

      var select = document.getElementById("class"); 
var classvalue = select.options[select.selectedIndex].value; 

     if (typeof XMLHttpRequest != "undefined"){ 
     xmlHttp= new XMLHttpRequest(); 
     } 
     else if (window.ActiveXObject){ 
     xmlHttp= new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     if (xmlHttp==null){ 
     alert("Browser does not support XMLHTTP Request") 
     return; 
     } 
     var url="studid.jsp"; 
     url +="?passdivision=" +encodeURI(str); 
     url +="&passclass=" +encodeURI(classvalue); 

     xmlHttp.onreadystatechange = studidchange; 
     xmlHttp.open("GET", url, true); 
     xmlHttp.send(null); 
     } 


     function studidchange(){ 
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
     document.getElementById("studid").innerHTML=xmlHttp.responseText 
     } 
     } 



     </script> 
     </head> 
     <body> 
     <table border="1"> 
     <tr><th>Class</th><th>Division</th><th>studid</th></tr> 
     <tr><td> 
     <select name='class' onchange="showdivision(this.value)"> 
     <option value="none">Select</option> 
    <% 
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/demo","root","password"); 
Statement stmt = con.createStatement(); 
ResultSet rs = stmt.executeQuery("Select class from class1"); 
while(rs.next()){ 
    %> 
     <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option> 
     <% 
} 
    %> 
     </select> 
     </td> 
     <td id='division'><select name='division' > 
     <option value='-1'></option> 
     </select> 
     </td> 
     <td id='studid'> <select name='studid' > 
     <option value='-1'></option> 
     </select> 
     </td> 
     </tr> 
     </table> 
     </body> 
     </html> 

Divisionn.jsp代碼..

<%@page import="java.sql.*"%> 
<% 
String classs=request.getParameter("passclassname"); 
String bu="<select name='division' onchange='showstudid(this.value);'><option value='-1'>Select</option>"; 
try{ 
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/demo","root","password"); 
Statement stmt = con.createStatement(); 
ResultSet rs = stmt.executeQuery("Select division_no from class1 where class='"+classs+"' "); 
    while(rs.next()){ 
    bu=bu+"<option value='"+rs.getString(1)+"'>"+rs.getString(1)+"</option>"; 
    } 
bu=bu+"</select>"; 
response.getWriter().println(bu); 
} 
catch(Exception e){ 
    System.out.println(e); 
} 

%> 

studid.jsp代碼...

<%-- 
    Document : divisionn 
    Created on : Nov 26, 2016, 11:13:27 AM 
    Author  : Jithin 
--%> 


<%@page import="java.sql.*"%> 
<% 
String division=request.getParameter("passdivision"); 

String classs=request.getParameter("passclass"); 



String bu="<select name='division'><option value='-1'>---Select--</option>"; 
try{ 
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/demo","root","password"); 
Statement stmt = con.createStatement(); 
ResultSet rs = stmt.executeQuery("Select studid from class1 where division_no='"+ division+"' and class='"+ classs+"' "); 
    while(rs.next()){ 
    bu=bu+"<option value='"+rs.getString(1)+"'>"+rs.getString(1)+"</option>"; 
    } 
bu=bu+"</select>"; 
response.getWriter().println(bu); 
} 
catch(Exception e){ 
    System.out.println(e); 
} 

%> 

此代碼不能正常工作..問題是通過選定類名的代碼..

function showstudid(str){ 

      var select = document.getElementById("class"); 
var classvalue = select.options[select.selectedIndex].value; 

     if (typeof XMLHttpRequest != "undefined"){ 
     xmlHttp= new XMLHttpRequest(); 
     } 
     else if (window.ActiveXObject){ 
     xmlHttp= new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     if (xmlHttp==null){ 
     alert("Browser does not support XMLHTTP Request") 
     return; 
     } 
     var url="studid.jsp"; 
     url +="?passdivision=" +encodeURI(str); 
     url +="&passclass=" +encodeURI(classvalue); 

     xmlHttp.onreadystatechange = studidchange; 
     xmlHttp.open("GET", url, true); 
     xmlHttp.send(null); 
     } 

任何人可以幫助我找到一個sollution ..謝謝了... ...

回答

1

要通過你將需要使用正確的URL編碼的URL傳遞多個參數。

var url="studid.jsp"; 
url +="?passdivision=" +encodeURI(str); 
url +="&passclass=" +encodeURI(xclass); 

需要注意的是額外的參數由&分離,encodeURI函數將URL編碼數據。

也考慮使用預準備語句,因爲您的SQL查詢存在明顯的SQL注入漏洞。

+0

該代碼正在工作,但我無法從下拉列表中選擇的值(Class值)。這是通過類名稱。 xclass值無法獲得。我已經更新了我的代碼,請參閱 – Jithin

+0

Jithin,你沒有在選擇框中設置你的id屬性,因爲你只設置了名字,所以getElementById只會返回null。將ID ='class'添加到您選擇的元素,它應該工作。 –

+0

非常感謝你,它運作良好.. :) – Jithin

相關問題