如何同时保存数据库中所有检索到的记录?

时间:2012-09-26 08:12:21

标签: javascript asp-classic

我有一个工资计算表,从检索数据库中的工资总额开始,然后,在用户添加一些额外的工资以计算净工资后,再次将它们保存回数据库。

我的问题是我的代码只能一次保存一条记录。我需要的是能够同时保存所有记录。那么请你帮助我。

<%
if Rs.eof then 
    response.write "<tr><td colspan=""9"">&nbsp;"
    call displayNotFoundRecord
    response.write "</td></tr>"
Else

    Do while Rs.AbsolutePage = strPageCurrent And Not Rs.EOF
    dim color 

            y = n mod 2
                if y > 0 then
                    color =  "EFF4FA"
                else 
                    color = "ffffff"
                end if                  

    if rs.fields.item("if_social_sec") = "True"  then
        displaytxt = "" 
        soc_sec_v = soc_sec
    else
        displaytxt = "none"
        soc_sec_v = 0
    end if 

    wage_v = rs.fields.item("Total")
    salary_v = rs.fields.item("lb_salary")

    if rs.fields.item("lb_type") = "perunit"  then
        salary_wage = wage_v
        displaytxt_w = "readonly class=""bgdisable""" 
        displaytxt_lb = "readonly class=""bgdisable"""      
    else
        salary_wage = salary_v
        displaytxt_w = ""
        displaytxt_lb = ""      
    end if

if_pm = request.form("if_pm")

pm_pay = rs.fields.item("lb_pmPay") 
    if if_pm <> "" then
        if_pm_v = pm_pay   
        disable_txt_pm = "readonly"     
    else
        if_pm_v = 0 
        disable_txt_pm = "readonly class=""bgdisable""" 
    end if  



%>
<form name="myform2_<%=n%>" action="salary_action.asp" method="POST">
<tr bgcolor="#<%=color%>"> 
<td class="btline difcursor" nowrap  width="7%">&nbsp;<%=rs.fields.item("lb_name")%></td>
<td class="btline center"  nowrap  width="8%"><input type="text" name="working_day" id="working_day" value="<%=rs.fields.item("MaxOfdays")%>" size="7" <%=displaytxt_w%> onFocus="startCalc(this);" onBlur="stopCalc(this);"></td>
<td class="btline "  nowrap width="10%"><input type="text" name="wage" id="wage" value="<%=salary_wage%>"  onFocus="startCalc(this);" onBlur="stopCalc(this);"></td>
<td class="btline center"  nowrap  width="8%"><input type="text" name="OT" id="OT" size="7" value="<%=if_OT_v%>" onFocus="startCalc(this);" onBlur="stopCalc(this);" <%=disabled_ot%>></td>
<td class="btline center" nowrap  width="6%" ><input type="text"  name="OT_rate" id="OT_rate" size="5" value="<%=rs.fields.item("lbOT")%>" <%'=disabled_txt%> readonly class="bgdisable"></td>
<td class="btline center" nowrap  width="6%" ><input type="text" name="OT_amt" id="OT_amt" size="5" value="" <%'=disabled_txt%>  readonly class="bgdisable"></td>
<td class="btline center" nowrap  width="8%"  ><input type="text" name="soc_sec" id="soc_sec" size="7" value="<%=soc_sec_v%>" <%=disable_txt_soc%> onFocus="startCalc(this);" onBlur="stopCalc(this);"></td>
<td class="btline center"  nowrap  width="8%"><input type="text" name="pmPay" id="pmPay" size="7" value="<%=if_pm_v%>" onFocus="startCalc(this);" onBlur="stopCalc(this);" <%'=disable_txt_pm%> readonly class="bgdisable"></td>
<td class="btline" nowrap style="padding-left: 10px" width="8%" ><input type="text" name="ex_pay" id="ex_pay" size="7" onFocus="startCalc(this);" onBlur="stopCalc(this);"></td>
<td class="btline bold " width="10%"><input type="text" name="net_wage" id="net_wage" size="7"  readonly class="bgdisable">
<input type="hidden" name="lb_type" id="lb_type" size="7" value="<%=rs.fields.item("lb_type")%>">
<input type="hidden" name="date_from" id="date_from" size="7" value="<%=date_from_txt%>">
<input type="hidden" name="date_to" id="date_to" size="7" value="<%=date_to_txt%>">
<input type="hidden" name="lb_id" id="lb_id" size="7" value="<%=rs.fields.item("lb_id")%>">
<input type="hidden" value="N" name="edit_salary">
</td>
<td class="btline"><input type="text" name="sar_note"  value="" size="14"></td>
<td class="btline"   > <input type="submit" value="Save1"></td>
</tr>


</form>
<%
        Rs.movenext
        n = n + 1       
    Loop
End if
Rs.close

set Rs=nothing
Call DBConnClose()
%>

<tr>
    <td colspan="12" align="center" style="padding:10px;">
            <input type="submit" value="Save2">
    </td>
</tr>

我需要的是让“Save2”工作。但是现在只有“Save1”才有用。

添加(我的脚本):

<script>
var intervals = {};
function startCalc(sender){ 
  var key = sender.form.name;
  intervals[key] = setInterval(function() {
    calc(key);
  },1);
}
function calc(key){
  var oForm = document.forms[key];

working_day = oForm.working_day.value;

  wage = oForm.wage.value;
  lb_type_v = oForm.lb_type.value;

    if (lb_type_v == "daily")
    {
        wage = wage * working_day;
    }
    else
    {
        wage = wage;
    }

  OT_rate = oForm.OT_rate.value;
  OT = oForm.OT.value; 
      OT_amt = OT_rate * OT;
  soc_sec = oForm.soc_sec.value; 
  ex_pay= oForm.ex_pay.value; 
  pmPay = oForm.pmPay.value; 

 net_wage = (wage * 1) + (OT_amt * 1) - (soc_sec * 1) + (ex_pay * 1) + (pmPay * 1);

  oForm.OT_amt.value = OT_amt;
  oForm.net_wage.value = net_wage.toFixed(2);
}

function stopCalc(sender){
  var key = sender.form.name;
  clearInterval(intervals[key]);
}
</script>

1 个答案:

答案 0 :(得分:1)

在循环中,您必须更改每个输入名称,使其唯一。您还应该更改ID以使其唯一,具体取决于您使用ID执行的操作,但如果一次检索所有记录值,则该名称非常重要。

最简单的方法是将数据库ID添加到名称和ID中。

因此,例如,更改此行...

<td class="btline center"  nowrap  width="8%"><input type="text" name="OT" id="OT" size="7" value="<%=if_OT_v%>" onFocus="startCalc(this);" onBlur="stopCalc(this);" <%=disabled_ot%>></td>

...到...

<td class="btline center"  nowrap  width="8%"><input type="text" name="OT_<%=rs.fields.item("lb_id")%>" id="OT_<%=rs.fields.item("lb_id")%>" size="7" value="<%=if_OT_v%>" onFocus="startCalc(this);" onBlur="stopCalc(this);" <%=disabled_ot%>></td>

(这样做是通过在数据库中添加下划线字符和lb_id值来使每个名称和ID唯一。)

然后在你检索你的值的脚本中(你没有发布这段代码所以我不确定它是什么样子),你需要打开相同的记录集并循环检查值。

所以在你可能有像...之类的行之前。

x =请求(“OT”)

你会做这样的事情......

'Open your recordset
Do while Rs.AbsolutePage = strPageCurrent And Not Rs.EOF
    x = Request("OT_" & Rs("lb_id"))
    '....all your other requests here
    'save to database here etc etc
Loop

好的,这是您修改的代码(查看评论,看看我搬家和删除的地方)......

<!--move form tag outside of table-->
<form name="myform2" action="salary_action.asp" method="POST"> 
<% 
if Rs.eof then  
    response.write "<tr><td colspan=""9"">&nbsp;" 
    call displayNotFoundRecord 
    response.write "</td></tr>" 
Else 

    Do while Rs.AbsolutePage = strPageCurrent And Not Rs.EOF 
    dim color  

            y = n mod 2 
                if y > 0 then 
                    color =  "EFF4FA" 
                else  
                    color = "ffffff" 
                end if                   

    if rs.fields.item("if_social_sec") = "True"  then 
        displaytxt = ""  
        soc_sec_v = soc_sec 
    else 
        displaytxt = "none" 
        soc_sec_v = 0 
    end if  

    wage_v = rs.fields.item("Total") 
    salary_v = rs.fields.item("lb_salary") 

    if rs.fields.item("lb_type") = "perunit"  then 
        salary_wage = wage_v 
        displaytxt_w = "readonly class=""bgdisable"""  
        displaytxt_lb = "readonly class=""bgdisable"""       
    else 
        salary_wage = salary_v 
        displaytxt_w = "" 
        displaytxt_lb = ""       
    end if 

if_pm = request.form("if_pm") 

pm_pay = rs.fields.item("lb_pmPay")  
    if if_pm <> "" then 
        if_pm_v = pm_pay    
        disable_txt_pm = "readonly"      
    else 
        if_pm_v = 0  
        disable_txt_pm = "readonly class=""bgdisable"""  
    end if   



%> 
<!--form tag moved outside of table-->
<tr bgcolor="#<%=color%>">  
<td class="btline difcursor" nowrap  width="7%">&nbsp;<%=rs.fields.item("lb_name")%></td> 
<td class="btline center"  nowrap  width="8%"><input type="text" name="working_day_<%=rs.fields.item("lb_id")%>" id="working_day_<%=rs.fields.item("lb_id")%>" value="<%=rs.fields.item("MaxOfdays")%>" size="7" <%=displaytxt_w%> onFocus="startCalc(<%=rs.fields.item("lb_id")%>);" onBlur="stopCalc(<%=rs.fields.item("lb_id")%>);"></td> 
<td class="btline "  nowrap width="10%"><input type="text" name="wage_<%=rs.fields.item("lb_id")%>" id="wage_<%=rs.fields.item("lb_id")%>" value="<%=salary_wage%>"  onFocus="startCalc(<%=rs.fields.item("lb_id")%>);" onBlur="stopCalc(<%=rs.fields.item("lb_id")%>);"></td> 
<td class="btline center"  nowrap  width="8%"><input type="text" name="OT_<%=rs.fields.item("lb_id")%>" id="OT_<%=rs.fields.item("lb_id")%>" size="7" value="<%=if_OT_v%>" onFocus="startCalc(<%=rs.fields.item("lb_id")%>);" onBlur="stopCalc(<%=rs.fields.item("lb_id")%>);" <%=disabled_ot%>></td> 
<td class="btline center" nowrap  width="6%" ><input type="text"  name="OT_rate_<%=rs.fields.item("lb_id")%>" id="OT_rate_<%=rs.fields.item("lb_id")%>" size="5" value="<%=rs.fields.item("lbOT")%>" <%'=disabled_txt%> readonly class="bgdisable"></td> 
<td class="btline center" nowrap  width="6%" ><input type="text" name="OT_amt_<%=rs.fields.item("lb_id")%>" id="OT_amt_<%=rs.fields.item("lb_id")%>" size="5" value="" <%'=disabled_txt%>  readonly class="bgdisable"></td> 
<td class="btline center" nowrap  width="8%"  ><input type="text" name="soc_sec_<%=rs.fields.item("lb_id")%>" id="soc_sec_<%=rs.fields.item("lb_id")%>" size="7" value="<%=soc_sec_v%>" <%=disable_txt_soc%> onFocus="startCalc(<%=rs.fields.item("lb_id")%>);" onBlur="stopCalc(<%=rs.fields.item("lb_id")%>);"></td> 
<td class="btline center"  nowrap  width="8%"><input type="text" name="pmPay_<%=rs.fields.item("lb_id")%>" id="pmPay_<%=rs.fields.item("lb_id")%>" size="7" value="<%=if_pm_v%>" onFocus="startCalc(<%=rs.fields.item("lb_id")%>);" onBlur="stopCalc(<%=rs.fields.item("lb_id")%>);" <%'=disable_txt_pm%> readonly class="bgdisable"></td> 
<td class="btline" nowrap style="padding-left: 10px" width="8%" ><input type="text" name="ex_pay_<%=rs.fields.item("lb_id")%>" id="ex_pay_<%=rs.fields.item("lb_id")%>" size="7" onFocus="startCalc(<%=rs.fields.item("lb_id")%>);" onBlur="stopCalc(<%=rs.fields.item("lb_id")%>);"></td> 
<td class="btline bold " width="10%"><input type="text" name="net_wage_<%=rs.fields.item("lb_id")%>" id="net_wage_<%=rs.fields.item("lb_id")%>" size="7"  readonly class="bgdisable"> 
<input type="hidden" name="lb_type_<%=rs.fields.item("lb_id")%>" id="lb_type_<%=rs.fields.item("lb_id")%>" size="7" value="<%=rs.fields.item("lb_type")%>"> 
<input type="hidden" name="date_from_<%=rs.fields.item("lb_id")%>" id="date_from_<%=rs.fields.item("lb_id")%>" size="7" value="<%=date_from_txt%>"> 
<input type="hidden" name="date_to_<%=rs.fields.item("lb_id")%>" id="date_to_<%=rs.fields.item("lb_id")%>" size="7" value="<%=date_to_txt%>"> 
<input type="hidden" name="lb_id_<%=rs.fields.item("lb_id")%>" id="lb_id_<%=rs.fields.item("lb_id")%>" size="7" value="<%=rs.fields.item("lb_id")%>"> 
<input type="hidden" value="N" name="edit_salary_<%=rs.fields.item("lb_id")%>"> 
</td> 
<td class="btline"><input type="text" name="sar_note"  value="" size="14"></td> 
<td class="btline"   > <!--submit button removed--></td> 
</tr> 


<!--form tag removed-->
<% 
        Rs.movenext 
        n = n + 1        
    Loop 
End if 
Rs.close 

set Rs=nothing 
Call DBConnClose() 
%> 

<tr> 
    <td colspan="12" align="center" style="padding:10px;"> 
            <input type="submit" value="Save2"> 
    </td> 
</tr> 

<!--move form tag after end of table-->
</form> 

...和你的JavaScript(我只是在记事本中这样做,所以希望它有效)...

<script> 
var intervals = {}; 
function startCalc(key){  
  intervals[key] = setInterval(function() { 
    calc(key); 
  },1); 
} 
function calc(key){ 
 // var oForm = document.forms[key]; 

working_day = document.getElementById("working_day_" + key).value; //oForm.working_day.value; 

  wage = document.getElementById("wage_" + key).value; //oForm.wage.value; 
  lb_type_v = document.getElementById("lb_type_" + key).value; //oForm.lb_type.value; 

    if (lb_type_v == "daily") 
    { 
        wage = wage * working_day; 
    } 
    else 
    { 
        wage = wage; 
    } 

  OT_rate = document.getElementById("OT_rate_" + key).value; //oForm.OT_rate.value; 
  OT = document.getElementById("OT_" + key).value; //oForm.OT.value;  
      OT_amt = OT_rate * OT; 
  soc_sec = document.getElementById("soc_sec_" + key).value; //oForm.soc_sec.value;  
  ex_pay= document.getElementById("ex_pay_" + key).value; //oForm.ex_pay.value;  
  pmPay = document.getElementById("pmPay_" + key).value; //oForm.pmPay.value;  

 net_wage = (wage * 1) + (OT_amt * 1) - (soc_sec * 1) + (ex_pay * 1) + (pmPay * 1); 

  document.getElementById("OT_amt_" + key).value = OT_amt; //oForm.OT_amt.value = OT_amt; 
  document.getElementById("net_wage_" + key).value = net_wage.toFixed(2); //oForm.net_wage.value = net_wage.toFixed(2); 
} 

function stopCalc(key){ 
  clearInterval(intervals[key]); 
} 
</script> 

您还需要修改表单提交的页面。希望你能按照我上面给出的说明做到这一点