How to get multiple Onchange with value from db in JSP

zamirjalil
zamirjalil used Ask the Experts™
on
Hello guys...

I want to get an multiple dropdown box with onchange events which the value for each box is getting from db.
The problem is I call my db using jdbc.

for example:

I want to generate a list of warehouse from several countries. I should display the country first (which getting from db) and user choose one country then the next dropdown box will ganerate a list of district. From the list of district user can choose the warehouses within the district. And click the warehouse will generate warehouse info.

I totally have no idea how to do this. Hope you can help.

I have a code from viperfoo which solve my previous problem below to get product detail from dropdown box. I want something like this but it should generate multiple dropdown box with onchange event. Hope you get you get an ida too.

here is the code from viperfoo (thanks alot viperfoo :))

---------------------------------

<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:postgresql://localhost/dbname";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;

Class.forName("org.postgresql.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "dbuser", "dbpass");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT productid,title,price,unit FROM product where bizid='"+ bizid +"'") ;
%>

<script language="javascript">
<!--
// declaring array to store values for textbox "price" and "unit"
var text_array = new Array();

var array_counter;
array_counter = 0;
//-->
</script>

<select name="item" onchange="javascript:genTextBoxValue(this)">
<%     while (rs.next()) { %>
<%          productid = rs.getString("productid"); %>
<%          title = rs.getString("title"); %>
<%          price = rs.getString("price"); %>
<%          unit = rs.getString("unit"); %>
<%           out.println("<option value=" +productid+  ">" +title+ "" ); %>

<script language="JavaScript">
<!--
// loading values into array on page load
text_array[array_counter] = "<%= productid %>,<%= price %>,<%= unit %>";
array_counter = array_counter + 1;
//-->
</script>

<%      }     %>
</select>

<%
rs.close();
%>


<script language="JavaScript">
<!--
// function to generate textbox "price" and "unit" value, base on dropdown "item" selection value
function genTextBoxValue(listobj)
{
 var criteria;
 criteria = listobj.value;
 
 if (criteria != "")
 {
      var i;
      document.all.price.value = "";
      document.all.unit.value = "";
     
      for (i=0;i<text_array.length;i++)
      {
           var temp_array;
           temp_array = text_array[i].split(",");

           if (temp_array[0] == criteria)
           {
                   document.all.price.value = temp_array[1];
                   document.all.unit.value = temp_array[2];
           }
      }
 }
}
//-->
</script>

<input type="text" name="price">
<!-- this price will be taken from db 'price' value -->
<input type="text" name="unit">
<!-- this unit also will be taken from db 'unit' value -->

-----------------------

thank you in advance

zamir
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this one this will give an idea... on how to approach the problem...
It is almost the same as whta you wan to do.... this is using request.querystring....

 
Cut and paste the script
====save this as datepage.asp====

<script language="Javascript">
function doSel(obj)
{
  for (i = 0; i < obj.length; i++)
     if (obj[i].selected == true)
       eval(obj[i].value);
}

</script>



<form name="form1" method="post" action="datepage.asp">

<select name="CbosDate" onchange="doSel(this)">
         <% FOR counter = 0 TO 62 %>
        <option value="location.href='datepage.asp?CbosDate=<%=FormatDateTime((Now()+counter), 2)%>'" <%if request.querystring("CbosDate")=FormatDateTime((Now()+counter), 2) then response.write "selected" end if%>> <%=FormatDateTime((Now()+counter), 1)%></option>
       <% NEXT %>
         </select>


<% if  request.querystring("CbosDate") <> "" then  %>
<SELECT size="1" name="CboDate">
        <% FOR counter = 0 TO 62 %>
        <OPTION VALUE="<%=FormatDateTime(Cdate(request.querystring("CbosDate")) + counter, 2)%>"><%=FormatDateTime(Cdate(request.querystring("CbosDate")) + counter, 1)%></OPTION>
        <% NEXT %>
</SELECT>
<%else%>
<SELECT size="1" name="CboDate">
        <OPTION VALUE="">  </OPTION>
</SELECT>

<%end if%>
</form>

=============================
or you can check this link...

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20398728.html

Author

Commented:
ohh I forgot to tell you that my code must be in JSP...
by teh way..i willlook at your code first . Thanks gladxml

Commented:
zamirjalil,

Anyway the logic is there using request.querystring... I hope you can convert it in jsp...

Happy programming...







Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial