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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sum field depending on drop down box

Status
Not open for further replies.

evr72

MIS
Dec 8, 2009
265
US
Hello,

Thanks to all your help I have been writing some neat scripts. Thank you!!!

I am stuck with a script that works well with one line but if I bring dynamic data it does not work at all and not sure what the problem might be.
I am using my javascript in an asp page.
I have a drop down box and depending on the value of the drop down box my other field should change here is the code that I have
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/pricdsn.asp" -->
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_pricdsn_STRING
Recordset1.Source = "SELECT * FROM AMFLIB.MBCWCPP where cwfvnb = 1090101 and cwaitx='0025'"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script type="text/javascript">
function startCalc4(){
  interval = setInterval("calc4()",1);
}
function calc4(){
  one = document.form1.prcbook.value;
  two = document.form1.percent.value; 
  document.form1.total.value = (one * 1) * (two * 1);
}
function stopCalc4(){
  clearInterval(interval);
}
</script>
<style type="text/css">
<!--
#Layer1 {
	position:absolute;
	left:26px;
	top:49px;
	width:150px;
	height:24px;
	z-index:1;
}
#Layer2 {
	position:absolute;
	left:36px;
	top:22px;
	width:166px;
	height:22px;
	z-index:2;
}
#Layer3 {
	position:absolute;
	left:19px;
	top:24px;
	width:174px;
	height:21px;
	z-index:3;
}
-->
</style>
</head>

<body>



<form id="form1" name="form1" method="post" action="">
  <p>
  </p>


  <p> </p>
    <p>
      <input type="text" name="itm" value="<%=(Recordset1.Fields.Item("CWAITX").Value)%>" />
      
  </p>

    <table border="1">
      <tr>

        <td>CWFVNB</td>
        <td>CWAENB</td>
        <td>CWBRCD</td>

      </tr>
      <% While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF)) %>
    
        
          <td>
          <input type="text" name="qty" value="<%=(Recordset1.Fields.Item("CWAJQT").Value)%>"/></td>
       
          <td><input name="prcbook" type="text" value="<%=(Recordset1.Fields.Item("CWKDVA").Value)%>" onFocus="startCalc4();" 

onBlur="stopCalc4();" /></td>
          <td><input type="text" name="total" value=""/></td>
		  <td><select name="percent">
        <option value="0"onFocus="startCalc4();" 

onBlur="stopCalc4();">select</option>
        <option value="1.10onFocus="startCalc4();" 

onBlur="stopCalc4();">10%</option>
        <option value="1.25onFocus="startCalc4();" 

onBlur="stopCalc4();">25%</option>
      </select></td>
        </tr>
        <% 
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  Recordset1.MoveNext()
Wend
%>
    </table>
</form>
</body>
</html>
This shows all records for item 0025. if I take the Recordset1.MoveNext() Wed and only show one record it works fine

any idea why???

Thanks!!
 
I almost wonder whether you've even looked at the code produced, or attempted to validate it.

Take this line for example. Do you really think it's valid code?

Code:
<option value="1.10onFocus="startCalc4();" onBlur="stopCalc4();">10%</option>

You have no closing quote for your value attribute whatsoever!

Fix your bad markup, and make sure your code validates.

Then, look into the "onchange" event of the select element... it's far better to use that, IMHO, than onfocus on an option element.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Number one:
Look at the rendered code after the ASP has been processed, so you know exactly what is going on. The easiest way to do this is to just click on view-> source in your Browser after the pager has loaded. That will show you the final HTML
code that's being produced by your ASP code.


Number Two: Fix your Drop Down its completely wrong, I'm surprised anything works in it:

Code:
[gray]<select name="percent">
        <option value="0"onFocus="startCalc4();"

onBlur="stopCalc4();">select</option>
[/gray]
        [green]<option value=[/green][red]"1.10onFocus="[/red][blue]startCalc4();"[/blue][gray]
onBlur="stopCalc4();">10%</option>
...
      </select>[/gray]

Basically what your drop down says there, is that the value for the option 10% is '[green]1.10onFocus=[/green]'.

That complete string is its value, so your likely getting some bizarre results form your calculations.

Number Three: You don;t mention how its wrong, or if any errors are produced, if you check the Error console in Firefox, or even the Error Icon in IE on the bottom left corner, it should give you some information as to what is wrong.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Oops seems I took to long color coding the line.

Hopefully that makes it clearer though.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Sorry about the faulty code before this is the updated code
This is the one that works with the first line. if I show all records it still only works it the first line only, it does not change the values of all the other "total" fieldsnot sure how to get around that
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../rma1/Connections/prc.asp" -->
<%
Dim rs
Dim rs_numRows

Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = MM_prc_STRING
rs.Source = "SELECT * FROM AMFLIB.MBCWCPP"
rs.CursorType = 0
rs.CursorLocation = 2
rs.LockType = 1
rs.Open()

rs_numRows = 0
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<SCRIPT type=text/javascript>
function startCalc4(){
  interval = setInterval("calc4()",1);
}
function calc4(){
  one = document.form.prcbook.value;
  two = document.form.percent.value; 
  document.form.total.value = (one * 1) * (two * 1);
}
function stopCalc4(){
  clearInterval(interval);
}
</SCRIPT>



</head>

<body>
<form action="" method="get" name="form" id="form">
<table width="83%" border="1">
  <tr>
    <td width="17%"><input name="Input" type="text" value="<%=(rs.Fields.Item("CWAITX").Value)%>" /></td>
    <td width="17%"><select name="percent">
      <option>select</option>
      <option value="1.10" onchange="startCalc4();">10%</option>
      <option value="1.25" onchange="startCalc4();">25%</option>
      <option value="1.50" onchange="startCalc4();">50%</option>
    </select></td>
    <td width="17%"> </td>
    <td width="6%"> </td>
    <td width="43%"> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td><input type="text" name="qty" value="<%=(rs.Fields.Item("CWAJQT").Value)%>" /></td>
    <td><input type="text" name="prcbook"value="<%=(rs.Fields.Item("CWKDVA").Value)%>" onfocus="startCalc4();" /></td>
    <td><input type="text" name="total" value=""/></td>
  </tr>
</table>





</form>
</body>
</html>
<%
rs.Close()
Set rs = Nothing
%>
 
Me said:
look into the "onchange" event of the select element... it's far better to use that, IMHO, than onfocus on an option element.

You seen to have put the onchange on the option element, which clearly will not work. Re-read my sentence above.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
BillyRayPreachersSon,

I am not really sure what you mean, should I use something else? sorry fom my ignorance but not understanding what you are trying to tell me
 
Dan means you should use the onChange event of the Select element, not of the <option> elements inside.

In other words:

Code:
<select name='percent' onChange="startCalc4();">
...
</select>

With that said, your calc function seems to only be addressing one field with the name of 'total', that is inside a form with the name of 'form'.

If you have more than one field with the same name, how are you assuming its going to know which one it has to address to change its value. It would only address the first one it finds, and modify that.

Or if your fields have different names, then your calc function only ever addresses the one with the name 'total'.

And again, use the view source from your browser and copy & paste the HTML that is produced after the ASP has been processed so we may see what's actually being delivered to the page, instead of the ASP code.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ahhhh I think I got it just not sure how to implement it

I need to add a diferent name for each field, the thing is that the fileds are dynamic I would need 10 numbers. So...
is there a way to make a dynamic field change name for example original field is "field" next field woudl be "field2, etc

woudl be something like
<input type="text" name="total(+1)" value=""/>

??
 
Have ASP dynamically number the names of input fields when its creating them.









----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you all I was able to get what I needed I really apreciate the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top