Need javascript to SUM listboxes

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   Need javascript to SUM listboxes
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 20 year's 20 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Gender: Male
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.02 posts per day
Posts: 160
Points: 9,216
   
I have a program I am working on that allows students to submit "Peer Evaluations" for their team members for a college course. They could have anywhere from 5 to 7 members per team which is pulled from a MySQL database.

The minimum score is 7 while the max is 13.

I have code that figures out what the sum should be but I would like a running total taken from the listboxes. The running total should show up in $SUM_Points.

I don't know how many students I would have per group. I was thinking of making the list boxes into an array and summing the array. If the sum of the array does not equal $Total_Points then they cannot submit the form.

If anybody can help me figure this out i would appreciate it.



function peerEvals() {
global $stop, $prefix, $db, $authuser;
include("header.php");
OpenTable();
$Course_ID = $_GET['Course_ID'];
$getcoursename = $db->sql_query("SELECT Course_Name FROM ".$prefix."_tl_courses WHERE Course_ID = '$Course_ID'");
if (!$getcoursename) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
      }

while($info = $db->sql_fetchrow($getcoursename)) {
$Course_Name  = $info[Course_Name];
}

echo"<table width=\"100%\" border=\"0\" cellpadding=\"3\">"
  ." <tr><td align=\"center\">Team Based Learning - Peer Evaluation</td></tr>" 
  ." <tr><td align=\"center\">Course: $Course_Name</td></tr>"
  . "</table>"; 
  echo"<table width=\"100%\" border=\"0\" cellpadding=\"3\">"
  ." <tr><td><b>Assigning Fair Peer Evaluation Scores<b></td></tr>"
  ."<tr><td><p>Assign an Average of 10 points to your other members of your team (ie NOT including yourself.) You should assign a total of 40 points for a five-member team or 50 points for a six-member team.  The minimum possible score for a team member is 7 and the maximum possible score is 13.  Assign scores to reflect how you feel about the extent to which the other member contributed to your learning and/or your teams performance.  This is an opportunity to reward the members of your teamn who actually worked hard on your behalf.  </p></td>";
  echo"<tr><td><b>Providing Helpful Feedback Comments<b></td></tr>"
  ."<tr><td><p>In addition to assigning peer evaluations scores, you must provide writtten peer feedback to EACH member of your team. When giving feedback, keep in mind the seven characteristcs of Helpful Feedback:</td></tr>";
  echo"<tr><td>"
  ."<OL>"
  ."<LI>Descriptive, not evaluative, and is &quot;owned&quot; by the sender."
  ."<LI>Specific, not general."
  ."<LI>Honest and sincere."
  ."<LI>Expressed in terms relevent to the self-perceived needs of the receiver."
  ."<LI>Timely and in context."
  ."<LI>Desired by the receiver, not imposed on him or her."
  ."<LI>Usable, concerned with behavior over which the receiver has control."
  ."</OL>"
  ."<b></td></tr>";
  echo"</table>";
  CloseTable();
  $findusergroup = $db->sql_query("SELECT Group_ID FROM ".$prefix."_tl_group_students WHERE LDAP_USER = '$authuser'");
  while($info = $db->sql_fetchrow($findusergroup)) {
$UserGroup = $info[Group_ID];
}
 
  OpenTable();
  echo"   <form id=\"form1\" name=\"form1\" method=\"post\" action=\"\">"
  . "     <table width='100%' border='1' cellpadding='3'>"
  . "       <tr><td colspan='3' align='center'>Group: $UserGroup</td><tr>"
  . "      <tr>"
  . "         <td>Team Member</td>"
  . "         <td>Score</td>"
  . "         <td>Helpful Feedback</td>"
  . "       </tr>";
  $getgroupmembers = $db->sql_query("SELECT s.Name_First, s.Name_Last, s.SOMS_KEY, s.UID, gs.LDAP_USER FROM ".$prefix."_tl_group_students gs LEFT JOIN ".$prefix."_tl_students s ON s.SOMS_KEY = gs.SOMS_KEY 
WHERE gs.Group_ID = '$UserGroup'
AND gs.LDAP_USER != '$authuser'");
while($row = $db->sql_fetchrow($getgroupmembers)) {
$SOMS_KEY = $row['SOMS_KEY'];
$UID = $row['UID'];
$Name_First = $row['Name_First'];
$Name_Last = $row['Name_Last'];
echo"<tr>"
    ."<td align='center'>&nbsp;$Name_First $Name_Last &nbsp;</td>"
    ."<td>"
    ."<select name='evalscore'>"
    ."<option value='7' selected>7</option>"
   ."<option value='8'>8</option>"
   ."<option value='9'>9</option>"
   ."<option value='10'>10</option>"
   ."<option value='11'>11</option>"
   ."<option value='12'>12</option>"
   ."<option value='13'>13</option>"
   ."</select>"
   ."</td>"   
    ."<td align='left'><textarea name ='evalcomment' cols='100' colspan='1' rows='2'></textarea></td>"   
    ."</tr>";
}
$countgroupmembers = $db->sql_query("SELECT * FROM ".$prefix."_tl_group_students WHERE gs.Group_ID = '$UserGroup'");
$grouptotal = $db->sql_numrows($countgroupmembers);
if ($grouptotal = 7) {
$Total_Points = 60;
} elseif ($grouptotal = 6) {
$Total_Points = 50;
} elseif ($grouptotal = 5)  {
$Total_Points = 40;
}

echo "<tr><td align='left'> Total Points Must = $Total_Points</td>";
echo "<td align='left'>$SUM_Points</td></tr>";
echo "<input type='hidden' name='SOMS_KEY' value='$SOMS_KEY'>\n";
echo "<input type='hidden' name='UID' value='$UID'>\n";
echo "<input type='hidden' name='Course_ID' value='$Course_ID'>\n";
echo "<tr><td colspan=\"3\" align=\"center\"><input type=\"Submit\" name=\"TLInsertPeerEvals\" value=\"Submit\"></td></tr>";   
  echo"</table>";
  echo"</form>"; 
CloseTable();
include("footer.php");
}


The TLInsertPeerEvals function will handle all the inserts to the database.

Tellii, been awhile... are you interested in doing some freelance work again?


Back to top Reply with quote
#2   
Telli
Site Admin
Occupation: Self Employed
Age: 46
Gender: Male
Fav. Sports Team: Detroit Red Wings
Website:
Status: Offline
Joined: May 26, 2003
1.03 posts per day
Posts: 8089
Points: 494,430
   
The evalscore is the one your trying to add up with Javascript and you want its value to show up where $SUM_Points is correct? You want the value to show there without refreshing the page?

Nice to see you around... I am always up for side work but you will have to wait in line.




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#3   
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 20 year's 20 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Gender: Male
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.02 posts per day
Posts: 160
Points: 9,216
   


Originally posted by Telli @ Fri Mar 07, 2008 5:00 pm:

The evalscore is the one your trying to add up with Javascript and you want its value to show up where $SUM_Points is correct? You want the value to show there without refreshing the page?


Correct.



Back to top Reply with quote
#4   
Telli
Site Admin
Occupation: Self Employed
Age: 46
Gender: Male
Fav. Sports Team: Detroit Red Wings
Website:
Status: Offline
Joined: May 26, 2003
1.03 posts per day
Posts: 8089
Points: 494,430
   
The evalscore and evalcomment have to get changed to a distinct name. I would suggest adding the UID on the end of them. So for example:


while($row = $db->sql_fetchrow($getgroupmembers)) {
$SOMS_KEY = $row['SOMS_KEY'];
$UID = $row['UID'];
$Name_First = $row['Name_First'];
$Name_Last = $row['Name_Last'];
echo"<tr>"
    ."<td align='center'>&nbsp;$Name_First $Name_Last &nbsp;</td>"
    ."<td>"
    ."<select name='evalscore_$UID'>"
    ."<option value='7' selected>7</option>"
   ."<option value='8'>8</option>"
   ."<option value='9'>9</option>"
   ."<option value='10'>10</option>"
   ."<option value='11'>11</option>"
   ."<option value='12'>12</option>"
   ."<option value='13'>13</option>"
   ."</select>"
   ."</td>"   
    ."<td align='left'><textarea name ='evalcomment_$UID' cols='100' colspan='1' rows='2'></textarea></td>"   
    ."</tr>";
}


Here is some javascript to add up the total points and an example of it working:


<head>
<script type="text/javascript">
//<![CDATA[
function sum_points() {
   var selectBox = document.getElementsByTagName('select');
   var total = 0;
   for(i=0; i<selectBox.length; i++) {
      total += Number(selectBox[i].options[selectBox[i].selectedIndex].value);
   }
   document.getElementById('total_points').innerHTML = 'Total Points: ' + total;
}
window.onload = sum_points;
//]]>
</script>
</head>
<body>
<form style="margin: 0px;">
<p>
<label for="evalscore_1">Points:</label> <select id="evalscore_1" name="evalscore_1" onchange="sum_points();">
   <option value='7' selected="selected">7</option>
   <option value='8'>8</option>
   <option value='9'>9</option>
   <option value='10'>10</option>
   <option value='11'>11</option>
   <option value='12'>12</option>
   <option value='13'>13</option>
</select>
<br />
<label for="evalcomment_1">Comment:</label> <textarea id="evalcomment_1" name="evalcomment_1" cols="60" rows="2"></textarea>
</p>

<p>
<label for="evalscore_2">Points:</label> <select id="evalscore_2" name="evalscore_2" onchange="sum_points();">
   <option value='7' selected="selected">7</option>
   <option value='8'>8</option>
   <option value='9'>9</option>
   <option value='10'>10</option>
   <option value='11'>11</option>
   <option value='12'>12</option>
   <option value='13'>13</option>
</select>
<br />
<label for="evalcomment_2">Comment:</label> <textarea id="evalcomment_2" name="evalcomment_2" cols="60" rows="2"></textarea>
</p>

<p>
<label for="evalscore_3">Points:</label> <select id="evalscore_3" name="evalscore_3" onchange="sum_points();">
   <option value='7' selected="selected">7</option>
   <option value='8'>8</option>
   <option value='9'>9</option>
   <option value='10'>10</option>
   <option value='11'>11</option>
   <option value='12'>12</option>
   <option value='13'>13</option>
</select>
<br />
<label for="evalcomment_3">Comment:</label> <textarea id="evalcomment_3" name="evalcomment_3" cols="60" rows="2"></textarea>
</p>

<p>
<label for="evalscore_4">Points:</label> <select id="evalscore_4" name="evalscore_4" onchange="sum_points();">
   <option value='7' selected="selected">7</option>
   <option value='8'>8</option>
   <option value='9'>9</option>
   <option value='10'>10</option>
   <option value='11'>11</option>
   <option value='12'>12</option>
   <option value='13'>13</option>
</select>
<br />
<label for="evalcomment_4">Comment:</label> <textarea id="evalcomment_4" name="evalcomment_4" cols="60" rows="2"></textarea>
</p>

</form>
<div id="total_points"></div>

</body>




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#5   re: Need javascript to SUM listboxes
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 20 year's 20 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Gender: Male
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.02 posts per day
Posts: 160
Points: 9,216
   
I don't understand the need for:

<label for="evalscore_1">Points:</label> <select id="evalscore_1" name="evalscore_1" onchange="sum_points();">
   
<label for="evalcomment_1">Comment:</label> <textarea id="evalcomment_1" name="evalcomment_1" cols="60" rows="2"></textarea>
...
<label for="evalscore_2">Points:</label> <select id="evalscore_2" name="evalscore_2" onchange="sum_points();">
 
<label for="evalcomment_2">Comment:</label> <textarea id="evalcomment_2" name="evalcomment_2" cols="60" rows="2"></textarea>
...

<label for="evalscore_3">Points:</label> <select id="evalscore_3" name="evalscore_3" onchange="sum_points();">
   
<label for="evalcomment_3">Comment:</label> <textarea id="evalcomment_3" name="evalcomment_3" cols="60" rows="2"></textarea>
...

<label for="evalscore_4">Points:</label> <select id="evalscore_4" name="evalscore_4" onchange="sum_points();">

etc


I don't know how many rows I need cause I won't know who many members will be in a group. I could have anywhere from 5 - 7 in a group.


Back to top Reply with quote
#6   
Telli
Site Admin
Occupation: Self Employed
Age: 46
Gender: Male
Fav. Sports Team: Detroit Red Wings
Website:
Status: Offline
Joined: May 26, 2003
1.03 posts per day
Posts: 8089
Points: 494,430
   
The need for what? You have a points dropdown and a comment textarea correct? Thats all I added. Its just an example of correct XHTML (strict).




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#7   
Telli
Site Admin
Occupation: Self Employed
Age: 46
Gender: Male
Fav. Sports Team: Detroit Red Wings
Website:
Status: Offline
Joined: May 26, 2003
1.03 posts per day
Posts: 8089
Points: 494,430
   
I take it you got this working ok?




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#8   
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 20 year's 20 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Gender: Male
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.02 posts per day
Posts: 160
Points: 9,216
   
No. I started working on another area of the system. Letting it stew in my head before I address it again. Could still use your help with some freelance work if your interested.



Back to top Reply with quote
#9   re: Need javascript to SUM listboxes
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 20 year's 20 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Gender: Male
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.02 posts per day
Posts: 160
Points: 9,216
   
As you can tell I don't have a clue what I'm doing with javascript. I found two examples I am going off of trying to put something together.

It won't let me post my examples. html tags not allowed.



Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum