By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

javascript add records in MS DB by using ASP vbscript recordset varible

P: 2
hi
help me i m so much stuck int he code and i have no time ....

i used ASP VBscipt and javascript functions with MS database

javascript function add records in MS DB by using ASP vbscript recordset varible

when user click the save button then some values save in the database so i write javascript function to add the records...

here is the code
---------------------------------------------------------------------------------------
2.asp
-------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. <!--#include file="con_db.asp" -->
  2.  
  3.  
  4. <%
  5.  
  6.  
  7.  
  8.  
  9.                             dim rs1
  10.                             set rs1= server.CreateObject("ADODB.recordset")
  11.                             rs1.ActiveConnection=DB_string
  12.                             rs1.Source="Select * from physical_usage_data"
  13.                             rs1.Cursortype=2
  14.                             rs1.locktype=3
  15.                             rs1.open()
  16.                             rs1_numRows = 0
  17.  
  18.                             dim rs2
  19.                             set rs2= server.CreateObject("ADODB.recordset")
  20.                             rs2.ActiveConnection=DB_string
  21.                             rs2.Source="Select * from product_data"
  22.                             rs2.Cursortype=1
  23.                             rs2.locktype=2 
  24.                             rs2.open()
  25.                             rs2_numRows = 0
  26.  
  27.  
  28. %>
  29. <script language="javascript">
  30.  
  31.         var d2_array=new Array;
  32.         var p2_array=new Array;
  33.         var P2=0;
  34.         var D_html;
  35. </script>
  36.  
  37. <script language="javascript">
  38.  
  39.  
  40. function showHint(p_id, p_val)
  41. {
  42. if (p_id=='Detergent')
  43.     {
  44.          p2_array[P2]=p_val;
  45.     <%
  46.     rs2.movefirst
  47.     if not(rs2.eof and rs2.bof) then
  48.         do until rs2.eof
  49.             for i=1 to rs2.recordcount
  50.     %>
  51.  
  52.                 if (p_val=='<%=rs2.fields.item("PRODUCT").value%>')
  53.                  {   
  54.                      p2_array[P2]=p_val;
  55.                      d2_array[P2]='<%=rs2.fields.item("PROD_ID").value%>'+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+'<%=rs2.fields.item("PROD_DESC").value%>';
  56.                      P2++;
  57.                  }
  58.     <%
  59.     rs2.movenext
  60.            next
  61.         loop
  62.     end if
  63.     %>
  64.                  D_html="<table id='Table2'>";
  65.                 for (i = 0; i<=d2_array.length-1; i++)
  66.                     {
  67.                         D_html+="<tr><td nowrap>";
  68.                         D_html+=d2_array[i];
  69.                         D_html+="&nbsp;&nbsp;&nbsp;</td><td><input type='text' size='10'>gallons &nbsp;&nbsp;&nbsp;&nbsp;";
  70.                         D_html+="</td><td><input type='button' value='Remove' onclick='P2_deleteRow(this)'></td></tr>";
  71.                     }
  72.                         D_html+="</table>";
  73.                         document.getElementById("Detergent").innerHTML=D_html;
  74.  
  75.  
  76.  
  77.     }
  78. }
  79.  
  80. function P2_deleteRow(r)
  81.   {
  82.   var i=r.parentNode.parentNode.rowIndex;
  83.   d2_array.splice(i,1);
  84.   p2_array.splice(i,1);
  85.   document.getElementById('Table2').deleteRow(i);
  86.    P2=d2_array.length;
  87.   }
  88.  
  89.  
  90.  
  91. ///////////////////////////////////////////////////////////////////////////////////////////
  92.  
  93. function save() {
  94. var record
  95.     <%rs2.movefirst%>
  96.     for (t = 0; t<=p2_array.length-1; t++){
  97.             record=p2_array[t];
  98.         <%if not(rs2.eof and rs2.bof) then
  99.             do until rs2.eof
  100.                 for i=1 to rs2.recordcount
  101.         %>
  102.  
  103.                     if (record == '<%=rs2.fields.item("PRODUCT").value%>')
  104.                      {   
  105.  
  106.                          <%
  107.                          rs1.AddNew
  108.                              rs1.fields("PROD_ID")=rs2.fields("PROD_ID").value      
  109.                          rs1.Update
  110.  
  111.                          %>
  112.                          document.getElementById("a").innerHTML="done";
  113.                      }
  114.         <%
  115.         rs2.movenext
  116.                next
  117.             loop
  118.         end if
  119.         %>
  120.   }
  121. }
  122.  
  123. </script>
  124.  
--------------------------------------------------------
here is the other file coding where i m callng these functions
---------------------------------------------------------------------------------------
1.asp
-------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. <%@LANGUAGE="VBSCRIPT"%>
  2. <% response.Buffer=true %>
  3. <!--#include file="2.asp" -->
  4.  
  5. <%
  6.                             dim rs,rs4                
  7.                             set rs= server.CreateObject("ADODB.recordset")
  8.                             rs.ActiveConnection=DB_string
  9.                             rs.Source="SELECT DISTINCT sales_data.PRODUCT, product_data.PROD_CATEGORY, sales_data.CUST_ACCT_ID FROM sales_data INNER JOIN product_data ON sales_data.PROD_ID = product_data.PROD_ID WHERE (((sales_data.CUST_ACCT_ID)= 'P11618675'))"
  10.                             rs.Cursortype=1
  11.                             rs.locktype=2 
  12.                             rs.open()
  13.                             rs_numRows = 0
  14.  
  15.  
  16.                             set rs4= server.CreateObject("ADODB.recordset")
  17.                             rs4.ActiveConnection=DB_string
  18.                             rs4.Source="SELECT DISTINCT physical_usage_data.PROD_ID, physical_usage_data.PROD_CATEGORY, product_data.PROD_DESC, physical_usage_data.PROD_USAGE_AMT, physical_usage_data.PROD_USAGE_DT, physical_usage_data.ENTER_BY_IND, physical_usage_data.CRE_DT FROM physical_usage_data INNER JOIN product_data ON physical_usage_data.PROD_ID = product_data.PROD_ID WHERE (((physical_usage_data.CUST_ACCT_NBR)= 'P11618675'))"
  19.                             rs4.Cursortype=1
  20.                             rs4.locktype=2 
  21.                             rs4.open()
  22.                             rs4_numRows = 0
  23.  
  24. %>
  25. <html>
  26. <head>
  27. <title>java</title>
  28. </head>
  29. <body>
  30.  
  31.  
  32.   <%
  33.             if not(rs4.eof and rs4.bof) then
  34.  
  35.                 rs4.movefirst
  36.                 response.write("<table id='myTable1'>")
  37.                 do until rs4.eof
  38.                      for i=1 to rs4.recordcount
  39.                         if (rs4.fields.item("PROD_CATEGORY").value)="Detergent" then
  40.  
  41.                             response.Write("<tr><td>"&rs4.fields.item("PROD_ID").value&"</td><td nowrap>&nbsp;&nbsp;&nbsp;&nbsp;"&rs4.fields.item("PROD_DESC").value&"</td><td>&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' id='txt' size='10' readonly='text' value='"&rs4.fields.item("PROD_USAGE_AMT").value&"'>gallons</td><td>&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Remove' onclick='deleteRow(this,myTable1.id)'></td></tr>")
  42.                                 'response.Write(rs4.fields.item("PROD_ID").value &"&nbsp;&nbsp;&nbsp;&nbsp;"&rs4.fields.item("PROD_DESC").value&"&nbsp;&nbsp;&nbsp;&nbsp;")
  43.                                 'response.Write("<input type='text' id='txt' size='10' value='"&rs4.fields.item("PROD_USAGE_AMT").value&"'/><br>")
  44.                                 'response.Write("<input type='button' value='Remove' ><br>")
  45.  
  46.                         end if
  47.                         rs4.movenext
  48.                      next
  49.                 loop
  50.                 response.write("</table>")
  51.             end if
  52.  
  53.             %> 
  54.             <a id="a"></a><br>
  55.             <a id="b"></a><br>
  56.   <a id="Detergent"></a><br>
  57.     <select id="Detergent" onchange="showHint(this.id,this.value)">
  58.                   <option>Choose Product..</option>
  59.                   <%
  60.             rs.movefirst
  61.             if not(rs.eof and rs.bof) then
  62.                 do until rs.eof
  63.                      for RD=1 to rs.recordcount
  64.                         if (rs.fields.item("PROD_CATEGORY").value)="Detergent" then
  65.                             %>
  66.                                     <option  value="<%=rs.fields.item("PRODUCT").value%>">    <%=rs.fields.item("PRODUCT").value%></option>
  67.                                 <%
  68.                         end if
  69.                         rs.movenext
  70.                      next
  71.                 loop
  72.             end if
  73.             %>
  74.   </select> 
  75. <input type="button" value="Save" onclick="javascript:save('true')"/>
  76. <%
  77. rs2.close
  78. set rs2=nothing
  79. rs1.close
  80. set rs1=nothing
  81. rs.close
  82. set rs=nothing
  83. rs4.close
  84. set rs4=nothing
  85. %>
  86.  
  87. </body>
  88. </html>
  89.  
-------------------------------------------------------------
reply me fast ........
thanks
ddirector
Nov 20 '07 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
JavaScript can't interact with a database directly.

Either use Ajax to make a request to the server to save the data or put the data in a form and submit the form to a server-side script which saves the data to the database.
Nov 21 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.