Jump to content

Display run-time calculation


togo

Recommended Posts

I have a couple of questions to ask about data input:I am trying to display a run-time calculation on the screen. I have use post/get to access the input data field & perform the calculation, but I cannot display the answer from the calling program in the field named "quantity"<--ViewItem.asp--><form method="get" action="viewItemCalculation.asp" name="viewItemCalculation"><input type="text" name="metres" size="4" ><br><br>Packet Quantity:    <%Response.Write(Session("quantity"))%></form><--ViewItemCalculation.asp--><script LANGUAGE="VBScript"><-- Option Explicitdim validation,pQuantityFunction viewItemCalculation_OnSubmitif trim(Request.Form("metres"))<>"" thenpQuantity=trim(Request.Form("metres"))/1.894validation = trueviewItemCalculation_OnSubmit = trueelsevalidation = falseviewItemCalculation_OnSubmit = falseend ifsession("quantity")=pQuantityresponse.redirect "comersus_viewItem.asp"End function</SCRIPT>2nd Question: Would data still get posted if I used the tab button to escape from that input field?Thanks for any helpBluebell

Link to comment
Share on other sites

It looks like you're writing out the session value before you set it. If the function you listed gets executed when the form gets submitted, then that function will set the session value. So since the session value gets set when the form gets submitted, the value isn't going to be available to print when you're writing the form out, it hasn't been calculated yet. I'm not sure what you're asking about the tab.

Link to comment
Share on other sites

Thanks for your response. I am now using javascript which has a lot of flexibility with data input, in fact you have now prompted my next question.How do I now deal with Carriage Return?This is the new code: <input type="text" name="metres" id="metres" size="4" onblur="validate()" /> <script type="text/javascript"> function validate() { var quantity=document.getElementById('metres').value; total.value = quantity/1.894; }</script>Total Quantity<input type="text" name="total" size="4" id="total" />The calculation works only now pressing the Tab key. I know very little about Javascript so I do not have a total understanding of its language commands.Would you know of a better solution?Thanks

Link to comment
Share on other sites

Thanks I have done that & I am now working on retaining the form data if carriage return is pressed.

Pressing the enter key in a form field will submit the form by default. You might want to use onchange or onkeyup instead of onblur, onkeyup will calculate after every keypress.
Link to comment
Share on other sites

  • 2 weeks later...

HelloHow would I display another calculation, e.g. Price, this time only between HTML tags. input type="text" name="metres" id="metres" size="4" onkeyup="validate()" / ><br><br><script type="text/javascript"> function validate() { var quantity=document.getElementById('metres').value; document.getElementById("total").value=Math.ceil(quantity/1.894); }</script>Total Quantity:    <input type="text" name="total" size="4" id="total" ><br><br>Price:        <%=pCurrencySign & Total %><br><br>However I try it I either lose the value in id="total" if I try & place it the on validate() or I cannot pick up any value via asp if I use for example request.form("total")Thanks for any help

Link to comment
Share on other sites

I'm not sure what the problem is. The code you have above will calculate a value and put the value in the total box when someone presses a key in the other box. What else are you trying to do? One thing you might want to add is to validate the value in quantity. You're assuming it's a number and using it in a division calculation, but you don't check whether or not it's actually a number.

Link to comment
Share on other sites

I'm not sure what the problem is. The code you have above will calculate a value and put the value in the total box when someone presses a key in the other box. What else are you trying to do? One thing you might want to add is to validate the value in quantity. You're assuming it's a number and using it in a division calculation, but you don't check whether or not it's actually a number.
Link to comment
Share on other sites

I think I've sorted the problem out, I used the .innerhtml command & included it in the onkeyup function:<script type="text/javascript"> function validate() { var quantity=document.getElementById('metres').value; document.getElementById("total").value=Math.ceil(quantity/1.894); document.getElementById('price').innerHTML = '-.--' }</script>Total:    <input type="text" name="total" size="4" id="total" /><br><br>Price:        <%=pCurrencySign & " " %><span id='price'</span><br><br>Many thanks for your repsonse

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...