p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Classic ASP Basics (http://p2p.wrox.com/forumdisplay.php?f=61)
-   -   Calculating text fields (http://p2p.wrox.com/showthread.php?t=38251)

jayman February 9th, 2006 07:37 AM

Calculating text fields
 
I am using Dreamweaver 8 and vbscript in asp pages.
I have text fields where a user will enter scores into each field.
I need the "total score" field to calculate the data entered into the other fields and to total up the marks as data is entered into each field.

For example on one page i have fields called StartWord,Typing,Deleting,Perform. Each field has a text box assigned to it.

I have an asp page that posts all submitted data to an access data base. Do i write code in the access database in one of the fields in design view or do i write some type of code in the asp page that submits the data?

Does anyone know how to go about doing this?


Dj Kat February 26th, 2006 10:22 AM

Hi jayman,

It's not really clear wat you want to do to me.
1. What do you want to do with the value's in the input fields.
2. What do you need the database for.

If you want to calculate the total score using only the input fields this is how I would do it.
you will need 2 pages.
1. The form page.
2. The request page with this page you
    - calculate the total score
    - display the total score
    - (optional) save this score to a database.

If I'm completely wrong about your question let me know and and I'll help.







__________________________________________________ ________
This is my junk I'm gona eat it

jayman February 27th, 2006 04:57 AM

Hi,

thanks for replying. I thought i was totally clear.
I have several text fields\boxes in a form. I want the last text box to display and calculate the total score when scores are entered in the other text boxes. That's it. The data is already submitted to a database when the form is submitted. I just want the total score box to display the total score and be submitted to the database just like all the other data.

I haven an action asp page that submits any data entered into the form to an access database. I just want to know do i write some sort of code into the same action page to calculate the total score.

Thanks

Jayman












Quote:

quote:Originally posted by Dj Kat
 Hi jayman,

It's not really clear wat you want to do to me.
1. What do you want to do with the value's in the input fields.
2. What do you need the database for.

If you want to calculate the total score using only the input fields this is how I would do it.
you will need 2 pages.
1. The form page.
2. The request page with this page you
    - calculate the total score
    - display the total score
    - (optional) save this score to a database.

If I'm completely wrong about your question let me know and and I'll help.







__________________________________________________ ________
This is my junk I'm gona eat it


Dj Kat February 27th, 2006 05:44 AM

Now I see,

You need javascript DOM.
This can calcutalte the numbers on the moment you enter the values.
I'll help you with it if you have trouble with javascript DOM.

Hint you'll need onKeyUp.

goodluck.


__________________________________________________ ________
This is my junk I'm gona eat it

ChrisScott February 27th, 2006 05:50 AM

Hi Jayman,

If you want to calculate the total client-side before posting, you can do this with client-side scripting.

Can you post the code for your form and your calculation requirements?

Cheers,

Chris


mat41 February 27th, 2006 07:18 PM

My JS is average but I found your objective useful for me to have a go. Try:

----cut n paste code: name your page addScoreswithJS.asp-----
<html>
 <head>
  <title>calculate client side values</title>
  <script>
  function validateScores()
  {
     if(document.theForm.s1.value == "")
     {
        alert("Please enter a score in box one");
        document.theForm.s1.focus();
        document.theForm.s1.select();
        return(false);
     }
     if(isNaN(parseInt(document.theForm.s1.value)))
     {
        alert("The value in box one must be an integer");
        document.theForm.s1.focus();
        document.theForm.s1.select();
        return(false);
     }
     if(document.theForm.s2.value == "")
     {
        alert("Please enter a score in box two");
        document.theForm.s2.focus();
        document.theForm.s2.select();
        return(false);
     }
     if(isNaN(parseInt(document.theForm.s2.value)))
     {
        alert("The value in box two must be an integer");
        document.theForm.s2.focus();
        document.theForm.s2.select();
        return(false);
     }
     document.theForm.posted.value = "y";
     return(true)
  }

  function addScores(scoreValue)
  {
     var scoreOne;
     scoreOne = (scoreValue);
     parseInt(scoreOne);
     parseInt(document.theForm.RunningTotal.value);
     (document.theForm.RunningTotal.value = (document.theForm.RunningTotal.value + parseInt(scoreOne)));
  }

  function resetValues()
  {
     document.theForm.s1.value='';
      document.theForm.s2.value='';
     document.theForm.RunningTotal.value='';
     document.theForm.posted.value = '';
  }
  </script>
 </head>
<body>
<%
   if request.form("posted") = "y" then
      response.write "<b>The form has been posted - your total is " & request.form("RunningTotal") & "</b>"
   else
      response.write "<B>Enter scores - the last box should calculate and display the runing total.</b>"
   end if
%>
<form name="theForm" method="post" action="addScoreswithJS.asp" onsubmit="return validateScores();">
<input type="hidden" name="posted" value="">
<table>
 <tr>
  <td>Score One:</td>
 </tr>
 <tr>
  <Td><input type="text" name="s1" value="<% if request.form("s1") <> "" then response.write request.form("s1") end if %>" size="5"></td>
 </tR>
 <tr>
  <td>Score Two:</td>
 </tr>
 <tr>
  <Td><input type="text" onClick="javascript:addScores(document.theForm.s1. value);" value="<% if request.form("s2") <> "" then response.write request.form("s2") end if %>" name="s2" size="5"></td>
 </tR>
 <tr>
  <td><B>Running Total</b></td>
 </tr>
 <tr>
  <td><input type="text" name="RunningTotal" value="<% if request.form("RunningTotal") <> "" then response.write request.form("RunningTotal") end if %>" size="5"></td>
 </tr>
 <tr>
  <td><input type="submit" name="Submit" onClick="javascript:addScores(document.theForm.s2. value);">&nbsp;&nbsp;<INPUT type="button" value="Reset Values" onClick="resetValues();"></td>
 </tr>
</table>
</form>
</body>
</html>
-------------------------------------------------------------

Wind is your friend
Matt

jayman March 2nd, 2006 10:46 AM

Hi Chris,

I have a one page form with about 10 sets of text boxes on it.
At the end of each set of text boxes there is a box called Total score. Which should display the total score once data is input into the other text boxes.

I have this code so far that works:

function calculate(What) {
    for (var i=1,Total_Score1=0;i<3;i++)
       Total_Score1 += What.elements['Textfield' + i].value - 0;
    What.Total_Score1.value = Total_Score1;

This formula calculates the total score in a text box called Total Score1. Problem im having is that all the other text boxes are called some different e.g Total score1, Total score2 etc.

The other textfields that the user inputs scores into are called textfield1 etc. So that the formula adds up the scores no matter what u put in the text box.

How do i get the formula to calculate the total score for text boxes with the name of Total_score2,Total_score3 etc.

Jayman



Quote:

quote:Originally posted by ChrisScott
 Hi Jayman,

If you want to calculate the total client-side before posting, you can do this with client-side scripting.

Can you post the code for your form and your calculation requirements?

Cheers,

Chris



ChrisScott March 2nd, 2006 10:57 AM

Hi Jayman,

Ok, I think I get what you mean.

What are the names of the text boxes that you are going to add up for Total_Score2, Total_Score3 etc - do they continue as Textfield3, Textfield4 and so on?

Does each Total_Score[n] have the same number of text boxes to add up?

Cheers,

Chris


mat41 March 2nd, 2006 06:20 PM

jayman - Does the code I posted not meet your requirement?

Wind is your friend
Matt

jayman March 3rd, 2006 06:06 AM

thanks for your efforts but that not quite what i was looking for.

Jayman

Quote:

quote:Originally posted by mat41
 jayman - Does the code I posted not meet your requirement?

Wind is your friend
Matt


All times are GMT -4. The time now is 06:10 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.