943,807 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 4174
  • JSP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 28th, 2009
-1

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Once you decide to follow MVC standard, you can contact me.
Secondly you should start learning about basic deployment with Tomcat
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jun 28th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Quote ...
Once you decide to follow MVC standard, you can contact me.
Secondly you should start learning about basic deployment with Tomcat
of course i follow mvc standard i'm using jbdc inside jsp here just for testing purpose i'm going to convert it to MVC standard later.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sriups is offline Offline
7 posts
since Jun 2009
Jun 28th, 2009
-1

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Click to Expand / Collapse  Quote originally posted by sriups ...
of course i follow mvc standard i'm using jbdc inside jsp here just for testing purpose i'm going to convert it to MVC standard later.
You should do it from beginning, that way you wouldn't be pulling double shift and same goes for us
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jun 29th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

the best answer is ajax and actually you do not to learn ajax just copy
the code i will gave and follow the guidlines copy and paste the attached file into your project folder :

1- copy and paste the attached file into your project folder


2- copy the following code and paste into the head of you jsp file

<script type="text/javascript" src="prototype.js"></script>
<script>
function item_code1(value){

var myAjax1=new Ajax.Request('itemcode1.jsp',{
method: 'post',postBody:'id='+value, onSuccess: function(transport) {
var itemcode1=$('itemcode1');
itemcode1.update(transport.responseText);
}
});
}

3- in your itemname combobox html make it exactly like follow

<select name="itemname1" size="1" id="first1" onchange="item_code1($('first1').value);">

4- copy and paste the following code where do you want the itemcode to be appeare

<div id="itemcode1"> </div>
5- now you just need to open new jsp file and copy write the following

jsp Syntax (Toggle Plain Text)
  1. <%@ page import="java.util.*" %>
  2. <%@ page import="java.sql.*" %>
  3.  
  4.  
  5. <%
  6. String id=request.getParameter("id");
  7. Vector v1,v2;
  8.  
  9. [COLOR="Red"]jmm[/COLOR].Database database = new jmm.Database(pageContext.getServletContext().getRealPath("/WEB-INF/config.txt")); // depend on yours
  10. database.jdbcConnect();
  11. String sql;
  12.  
  13. sql = "select itemcode from item where itemname='"+id+"'";
  14.  
  15.  
  16.  
  17. out.print("<select name=itemcode1 id=\"itemcode \">");
  18.  
  19. v1=database.jdbcMultipleRowQuery(sql);
  20. if(v1.size()!=0){
  21. for(int i=0;i<v1.size();i++){
  22. v2 = (Vector)v1.elementAt(i);
  23. for(int j=0;j<v2.size();j++){
  24. out.print("<option>"+v2.get(0)+"</option>");
  25. }
  26. }
  27. out.print("</select>");
  28.  
  29. }else{
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36. database.jdbcConClose();
  37. database = null;
  38.  
  39. %>


5- i wish it will help ,
Attached Files
File Type: js prototype.js (97.3 KB, 8 views)
Last edited by peter_budo; Jun 29th, 2009 at 4:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
CoSIS1 is offline Offline
22 posts
since Jun 2009
Jun 29th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

just inform me if it does not work
Reputation Points: 10
Solved Threads: 1
Newbie Poster
CoSIS1 is offline Offline
22 posts
since Jun 2009
Jun 29th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

CoSIS1>the best answer is ajax and actually you do not to learn ajax just copy the code i will gave and follow the guidlines copy and paste the attached file into your project folder.

How did you know AJAX? just copy the code?

Wrap up source code with bb code tags.
Read
How to use BB code tags?
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 29th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

am new member here, and i just wanted to help here
plus this code is working , i have used it before .
actually i know some in ajax that helped me before in my projects
Reputation Points: 10
Solved Threads: 1
Newbie Poster
CoSIS1 is offline Offline
22 posts
since Jun 2009
Jun 29th, 2009
0

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

JSP Syntax (Toggle Plain Text)
  1. <html>
  2. <head><title>Sales Page/title></head>
  3. <body>
  4. <script type="text/javascript">
  5. function ajaxFunction()
  6. {
  7. var xmlHttp;
  8. try
  9. { // Firefox, Opera 8.0+, Safari
  10. xmlHttp=new XMLHttpRequest();
  11. }
  12. catch (e)
  13. { // Internet Explorer
  14. try
  15. {
  16. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  17. }
  18. catch (e)
  19. {
  20. try
  21. {
  22. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  23. }
  24. catch (e)
  25. {
  26. alert("Your browser does not support AJAX!");
  27. return false;
  28. }
  29. }
  30. }
  31.  
  32. xmlHttp.onreadystatechange=function()
  33. {
  34. //alert("i m not in ready state");
  35.  
  36. if(xmlHttp.readyState==0)
  37. {
  38. //document.myForm.time.value=xmlHttp.responseText;
  39. alert("The request is not initialized");
  40. }
  41. if(xmlHttp.readyState==1)
  42. {
  43. //document.myForm.time.value=xmlHttp.responseText;
  44. alert("The request has been set up");
  45. }
  46. if(xmlHttp.readyState==2)
  47. {
  48. //document.myForm.time.value=xmlHttp.responseText;
  49. alert("The request has been sent");
  50. }
  51. if(xmlHttp.readyState==3)
  52. {
  53. //document.myForm.time.value=xmlHttp.responseText;
  54. alert("The request is in process");
  55. }
  56. if(xmlHttp.readyState==4)
  57. {
  58. //document.myForm.time.value=xmlHttp.responseText;
  59. //alert("i m in ready state");
  60. document.getElementById("theResponse").innerHTML = xmlHttp.responseText;
  61. }
  62. }
  63.  
  64.  
  65. var itemname=document.myForm.itemname.value;
  66. var url="getProduct.jsp";
  67. url=url+"?itemname="+itemname+"&sid="+Math.random(); //url=url+"&sid="+Math.random();
  68.  
  69. xmlHttp.open("GET",url,true);
  70. xmlHttp.send(null);
  71. }
  72. </script>
  73. select itemname:
  74. <form name="myForm">
  75. /*
  76.   wrong code------
  77.   <option>processors</option>
  78. <option>speakers</option>
  79. <option>monitor</option>
  80. <option>mouse</option>
  81.  
  82. due to this code you can not get
  83. the value in the request.getParameter("itemname");
  84.  
  85. */
  86.  
  87. <!-- right code --->
  88.  
  89. <select name="itemname" onChange="ajaxFunction();">
  90. <option value="processors">processors</option>
  91. <option value="speakers">speakers</option>
  92. <option value="monitor">monitor</option>
  93. <option value="mouse">mouse</option>
  94.  
  95.  
  96.  
  97.  
  98.  
  99. </select>
  100. <div id="theResponse">
  101.  
  102. </div>
  103. </form></body>
  104. </html>

Now your second page getproduct.jsp

i have removed un necessary code

JSP Syntax (Toggle Plain Text)
  1.  
  2. <%@ page language="java" contentType="text/html" import="java.sql.*" %>
  3.  
  4. <html>
  5. <head><title>select box demos</title></head>
  6. <body>
  7.  
  8. <%
  9. try
  10. {
  11. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  12. Connection conn=DriverManager.getConnection("jdbc:odbc:select");
  13. try
  14. {
  15.  
  16. String val=request.getParameter("itemname");
  17.  
  18. //String val1="speakers";
  19. //String val2="processors";
  20.  
  21.  
  22. // String itname[]={};
  23.  
  24. // int i=0,j=0,m=0;
  25.  
  26. String str="select * from stocks where item_name=?";
  27.  
  28. PreparedStatement stmt=conn.prepareStatement(str);
  29.  
  30. stmt.setString(1,val);
  31.  
  32. ResultSet rs=stmt.executeQuery();
  33.  
  34. /* itname=new String[100];
  35.  
  36.   while(rs.next())
  37.   {
  38.   itname[m]=rs.getString(3);
  39.   m++;
  40.   }
  41.  
  42.  
  43.   for(j=m;j<100;j++)
  44.   {
  45.   itname[j]="null";
  46.   } */
  47.  
  48. %>
  49.  
  50. Prod Name: <select name="productname">
  51. <%
  52. while(rs.next())
  53. {
  54. // if(itname[i]!="null")
  55. out.println("<option value="+rs.getString(3)+">"+rs.getString(3)+"</option>");
  56. //i++;
  57. }
  58. // out.println("</select>");
  59.  
  60. }
  61. catch(SQLException e)
  62. {
  63. out.print("SQL Not Executing");
  64. }
  65. }
  66. catch(Exception e)
  67. {
  68. e.printStackTrace();
  69. }
  70.  
  71. finally
  72. {
  73.  
  74.  
  75. try
  76. {
  77. if(rs!=null)
  78. {
  79. rs.close();
  80. rs=null;
  81.  
  82. }
  83. if(stmt!=null)
  84. {
  85. stmt.close();
  86. stmt=null;
  87.  
  88. }
  89.  
  90. if(conn!=null)
  91. {
  92. conn.close();
  93. conn=null;
  94.  
  95. }
  96.  
  97.  
  98. }catch(SQLException e)
  99. {
  100. out.print("SQL Not Executing"+e);
  101. }
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109.  
  110. %>
  111. </select>
  112.  
  113. </body>
  114. </html>
Reputation Points: 2
Solved Threads: 1
Newbie Poster
amarjeetsingh is offline Offline
21 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: sending an email with pdf file everyday
Next Thread in JSP Forum Timeline: how can i add css in jsp website in dreamwiever 8





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC