Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Multiple Rows

Status
Not open for further replies.

BrendaD

Programmer
Jan 26, 2001
25
CA
I am having a horrible time trying to figure out what is wrong with my code...

I have a page where a voter (who has 10 votes) can place from 1 to 10 votes for each project - 27 projects in all.

I have figured out how to do batch updates to the database from an ASP page but can't get the Javascript to do it's bit.

Here is the Javascript:
Code:
function doCalcVote() { //calculates total votes
 var input1 = parseInt(document.all.VotesToDate.value);
 var input2 = parseInt(document.all.NewVotes.value);
 var myTotal = parseInt(input1 + input2);
 document.all.Votes.value = myTotal;
 
 
}
Here is the VBScript:

Code:
<form action="TestConfirm.asp" method="Get" name="form1">
 
  <p>
    <input type="submit" name="Submit" value="Vote">
  </p>
  <table width="50%"  border="2" cellspacing="2" cellpadding="2">
    <tr>
      <td>CHU</td>
      <td>Total Votes </td>
      <td>New Votes </td>
    </tr>
    <% 
While ((Repeat1__numRows <> 0) AND (NOT rsCHUVotes.EOF)) 
%>
    <tr>
      <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" value="<%=(rsCHUVotes.Fields.Item("pkBudgetItem").Value)%>"></td>
      <td><input name="VotesToDate" type="hidden" value="<%=(rsCHUVotes.Fields.Item("Votes").Value)%>"></td>
      <td>
        <input name="NewVotes" type="text" id="NewVotes"   onChange="doCalcVote()"  value="0" ><input name="Votes" type="hidden" value="">
      </td>
    </tr>
    <% 
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  rsCHUVotes.MoveNext()
Wend
%>

    </table>
</form>

When I pass the data onto the Confirm page (where I would do the batch update from) I am getting nothing.

The URL (I'm using GET so I can see what is being passed) has values for VotesToDate, NewVotes and pkBudgetItem but NOT for Votes.

It works when I just use a single record on the page so I feel the problem is that it isn't working with the repeat rows but I don't know....

Any help would be greatly appreciated!!!

TIA, Brenda
 
Hi Brenda,

can you post the generated source code instead of your ASP? Just do a view-source of the page and post it here. This will allow us to copy/paste the code in our own browser windows to test it.

I can tell you one thing right up front that I would change about your code (but it still probably won't solve your problem). Do not use the document.all collection. This is an IE only directive, which means that your script will break in almost every other browser. Instead assign ids to your HTML elements and access them via the document.getElementById("elementID") command.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
I appreciate your comments about document.all but this is an internal application that only runs on IE so it is not a grave concern.

Here is the code (Dreamweaver was used for most of it, obviously!):

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim MM_connBudgetVote_STRING
MM_connBudgetVote_STRING = "Provider=SQLOLEDB.1;Password=vipa7;Persist Security Info=True;User ID=aspdev7;Initial Catalog=RecBudget;Data Source=DB1"
%>
<%
Dim rsCHUVotes
Dim rsCHUVotes_numRows

Set rsCHUVotes = Server.CreateObject("ADODB.Recordset")
rsCHUVotes.ActiveConnection = MM_connBudgetVote_STRING
rsCHUVotes.Source = "SELECT * FROM dbo.vwCHUVotes"
rsCHUVotes.CursorType = 0
rsCHUVotes.CursorLocation = 2
rsCHUVotes.LockType = 1
rsCHUVotes.Open()

rsCHUVotes_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
rsCHUVotes_numRows = rsCHUVotes_numRows + Repeat1__numRows
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>Vote</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<SCRIPT language="Javascript">

function disableEnterKey() //disables Enter Key when function called
    {
      return !(window.event && window.event.keyCode == 13);
    }
				


function doCalcVote() { //calculates total votes
 var input1 = parseInt(document.all.VotesToDate.value);
 var input2 = parseInt(document.all.NewVotes.value);
 var myTotal = parseInt(input1 + input2);
 document.all.Votes.value = myTotal;
 }
 
 function UpdateVotes()
 {
 document.form1.action="ConfirmVotes.asp";
 document.form1.submit();
 
 
}
</script>

<body>
<form name="form1" method="get" action="ConfirmVote.asp">
  <table width="50%"  border="2" cellspacing="2" cellpadding="2">

    <tr>
      <td>CHU</td>
      <td>Votes
      <input type="submit" name="Submit" value="Vote" ></td>
    </tr>
<% Dim iCount 
iCount = 0

%>
<% 
While ((Repeat1__numRows <> 0) AND (NOT rsCHUVotes.EOF)) 
%>
<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="<%=(rsCHUVotes.Fields.Item("pkBudgetItem").Value)%>"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="<%=(rsCHUVotes.Fields.Item("Votes").Value)%>">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>
<% 
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  rsCHUVotes.MoveNext()
Wend
%>
<%
iCount = iCount + 1
%>
<tr>
      <td colspan="2">	    <input name="Count" type="hidden" value="<%=iCount -1 %>">
      </td></tr>
  </table>
</form>
</body>
</html>
<%
rsCHUVotes.Close()
Set rsCHUVotes = Nothing
%>

Thanks!
 
myself said:
can you post the generated source code [!]instead of your ASP[/!]? Just do a [!]view-source of the page[/!] and post it here.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Thank you for your time!

Here's the view source code:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>Vote</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<SCRIPT language="Javascript">

function disableEnterKey() //disables Enter Key when function called
    {
      return !(window.event && window.event.keyCode == 13);
    }
				


function doCalcVote() { //calculates total votes
 var input1 = parseInt(document.all.VotesToDate.value);
 var input2 = parseInt(document.all.NewVotes.value);
 var myTotal = parseInt(input1 + input2);
 document.all.Votes.value = myTotal;
 }
 
 function UpdateVotes()
 {
 document.form1.action="ConfirmVotes.asp";
 document.form1.submit();
 
 
}
</script>

<body>
<form name="form1" method="get" action="ConfirmVote.asp">
  <table width="50%"  border="2" cellspacing="2" cellpadding="2">

    <tr>
      <td>CHU</td>
      <td>Votes
      <input type="submit" name="Submit" value="Vote" ></td>
    </tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="1"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="20">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="2"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="21">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="3"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="10">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="4"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="9">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="5"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="8">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="6"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="7">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="7"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="6">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="8"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="5">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="9"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="4">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="10"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="3">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="11"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="2">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="12"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="1">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="13"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="11">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="14"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="12">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="15"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="13">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="16"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="14">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="17"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="15">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="18"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="16">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="19"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="17">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="20"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="18">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="21"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="19">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="22"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="2">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="23"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="3">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="24"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="5">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="25"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="6">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="26"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="8">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
    <td><input name="pkBudgetItem" type="text" id="pkBudgetItem" readonly="True" value="27"></td>
    <td>
        <input name="VotesToDate" type="text" id="VotesToDate" value="9">
        <input name="NewVotes" type="text" id="NewVotes" onKeyPress="return disableEnterKey()" value="0" >
      <input name="Votes" type="hidden" id="Votes">      </td>
</tr>

<tr>
      <td colspan="2">	    <input name="Count" type="hidden" value="0">
      </td></tr>
  </table>
</form>
</body>
</html>
 
Your problem is that you are trying to do one value assignment to multiple elements. You have to run something like this in a loop. You can not just assign all the votes textboxes with 1 command. Change your function to this:
Code:
function doCalcVote() { //calculates total votes
   var vtd = document.getElementsByTagName("VotesToDate");
   var nv = document.getElementsByTagName("NewVotes");
   var v = document.getElementsByTagName("Votes");
   for (var i = 0; i < vtd.length; i++) {
      v[i].value = parseInt(vtd[i].value, 10) + parseInt(nv[i].value, 10);
   }
}

And, just because you are on an intranet application does not mean that you should use IE only commands. What if your company decides to start supporting FF or Opera all of a sudden? Wouldn't it be nice to be ahead of the curve so that you don't have to recode anything? Or maybe you decide to change jobs to a new company that wants to support all browsers. It would benefit you as an individual to know how to code for all browser to make yourself more accessible to more companies (and would probably benefit your salary).

Not to mention, most of the time IE only commands can be accomplished with a similar command that works across a multitude of browsers. For example, document.getElementsByName("blah") is the exact same as document.all.blah, except that getElementsByName will work in FF and Opera as well. It's a little bit more typing, but that's a small price to pay for cross browser compatibility. If there's something that will functionally behave the same way, why would you ever want to use an IE only command?

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
You've to make some reference to the specific rows. Besides, naming all the input same name and id same value, may not be an excellent idea, in particular id's. Make the following change client-side. But, when the form is posted back, your server-side response script has to be modified accordingly.

[1] The form fields.
[tt]
<tr>
<td><input name="pkBudgetItem[blue]_<%= Repeat1__index %>[/blue]" type="text" id="pkBudgetItem[blue]_<%= Repeat1__index %>[/blue]"[blue] idx="<%= Repeat1__index %[/blue]">
value="<%=(rsCHUVotes.Fields.Item("pkBudgetItem").Value)%>"></td>
<td><input name="VotesToDate[blue]_<%= Repeat1__index %>[/blue]"[blue] idx="<%= Repeat1__index %[/blue]" type="hidden" value="<%=(rsCHUVotes.Fields.Item("Votes").Value)%>"></td>
<td>
<input name="NewVotes[blue]_<%= Repeat1__index %>[/blue]" type="text" id="NewVotes[blue]_<%= Repeat1__index %>[/blue]"[blue] idx="<%= Repeat1__index %>"[/blue] onChange="doCalcVote([blue]this.getAttribute('idx')[/blue])" value="0" ><input name="Votes[blue]_<%= Repeat1__index %>[/blue]"[blue] idx="<%= Repeat1__index %>"[/blue] type="hidden" value="">
</td>
</tr>
[/tt]
[2] The js function
[tt]
function doCalcVote([blue]sidx[/blue]) { //calculates total votes
var input1 = parseInt(document.[blue]form1.elements["VotesToDate+"_"+sidx][/blue].value,10);
var input2 = parseInt(document.[blue]form1.elements[NewVotes+"_"+sidx][/blue].value,10);
var myTotal = parseInt(input1 + input2, 10);
document.[blue]form1.elements[Votes+"_"+sidx][/blue].value = myTotal;
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top