Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asptoday_discuss thread: adding two text box values dynamically and getting the result in the third text box within a form


Message #1 by "john calvin" <john_calvin_wg@y...> on Thu, 6 Dec 2001 06:09:38
ITI

Bangalore

Dec 6, 2001



Hi,





I have written a asp code in which there is a form (spreadsheet) in which 

the text box name is take from the database.



I have to add two text box values and put the result in the third text 

box. Since the text box is inside a loop. I am taking the name of the text 

box from the database so that it is dynamic. when i call the function 

(javascript or vbscript)it is giving an syntax error. 

For the past one days i am stuck with this problem. please try to help me 

in solving this problem as soon as possible.





Finding problem in :



1. 	Capturing the name from the database for each text box which is in 

a 									

			

	loop.

	

	Example :

	

	vars1 =rs("prod_name")& "b1" // this is from the database

	Response.write("</td><td><input type=text name='vars1' size=7 

value='0' >")



2.	capturing the values from those text boxes

3. 	adding and displaying the value in the third text box (It can be 

in javascript or VBscript).

<SCRIPT LANGUAGE="VBScript">



<!--

	Sub add()

	dim sum

	sum =cint(Document.f1.vars2.value)+cint(Document.f1.vars3.value) 

//vars2, vars3 is giving an error 

	Document.f1.vars1.value = sum

	End Sub

-->

While adding using the above function i am unable to access the text box 

value using its name.



*****************

PROGRAM

________



<html>

<head>



<SCRIPT LANGUAGE="VBScript">

<!--

	Function Add()

	Add =cint(Document.f1.vars2.value) + cint(Document.f1.vars3.value)

	Document.f1.vars1.value=Add

	End Function

-->

</SCRIPT>



</head>

<body bgcolor =ffffcf>

<% Dim con, rs, count

    dim cat, vars1, vars2, vars3

	cat=" "

	count=0

    set con = Server.CreateObject("ADODB.Connection")

    con.Open "DSN=ro" 



	set rs=con.Execute("SELECT DISTINCTROW category.cat_name, 

products.prod_name FROM category INNER JOIN products ON category.cat_code 

= products.cat_code ORDER BY cat_rank, prod_rank;")

 

	response.write("<CENTER><form name=f1 method=post 

action='http://localhost/asp/ro/add.asp'><table border=1><tr bgcolor=pink 

align=middle><th><font size=3 color=black>PRODUCTS</th><th><font size=2 

color=black>Target_Yearly</th><th><font size=2 

color=black>Target_Apr</th><th><font size=2 

color=black>Target_May</th><th><font size=2 

color=black>Target_Jun</th><th><font size=2 

color=black>Target_Jul</th><th><font size=2 

color=black>Target_Aug</th><th><font size=2 

color=black>Target_Sep</th><th><font size=2 

color=black>Target_Oct</th><th><font size=2 

color=black>Target_Nov</th><th><font size=2 

color=black>Target_Dec</th><th><font size=2 

color=black>Target_Jan</th><th><font size=2 

color=black>Target_Feb</th><th><font size=2 

color=black>Target_March</th><th><font size=2 

color=black>Target_Yearly</th></font>")

	 

  	while not rs.EOF 

	

			If(not rs("cat_name")=cat) then

			Response.write("<tr align=middle><td><b><u>" & rs

("cat_name")& "</b></u><br>")

			End If

			cat=rs("cat_name")



		Response.write("<tr align=middle><td>" & rs("prod_name")) 



		vars1 =rs("prod_name")& "b1" 

		

		Response.write("</td><td><input type=text name="& vars1 &" 

size=7 onClick =Add()>")

		

		vars2 =rs("prod_name")& "b2"

		

		Response.write("<td><input type=text name='"& vars2 &"' 

size=7 value='0'>")

		

		vars3 =rs("prod_name")& "b3"		

		Response.write("<td><input type=text name='"& vars3 &"' 

size=7 value='0'>")

		

		

		

		

		

		Response.write("<td><input type=text name='" & rs

("prod_name")& "b4" & "' size=7 value='0'>")

		

		Response.write("<td><input type=text name='" & rs

("prod_name")& "b5" & "' size=7 value='0'>")

		

		Response.write("<td><input type=text name='" & rs

("prod_name")& "b6" & "' size=7 value='0'><td><input type=text name='" & rs

("prod_name")& "b7" & "' size=7 value='0'><td><input type=text name='" & rs

("prod_name")& "b8" & "' size=7 value='0'><td><input type=text name='" & rs

("prod_name")& "b9" & "' size=7 value='0'><td><input type=text name='" & rs

("prod_name")& "b10" & "' size=7 value='0'><td><input type=text name='" & 

rs("prod_name")& "b11" & "' size=7 value='0'><td><input type=text name='" 

& rs("prod_name")& "b12" & "' size=7 value='0'><td><input type=text 

name='" & rs("prod_name")& "b13" & "' size=7 value='0'><td><input 

type=text name='" & rs("prod_name")& "b14" & "' size=7 value='0'></tr>")

		

		rs.MoveNext

		Wend

	

		response.write("</table>")

				

	   	response.write("<br>") 	

		response.write("<br>")

	  	Response.write("<input type=submit 

value='SUBMIT'color=red> 

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<in
pu

t type=reset value='RESET'></form></CENTER>")

  



	con.close

	set rs=nothing

	set con=nothing %>	



</body>

</html>



*******************



Awaiting for your reply



With love,



calvin
Message #2 by Subha Gowri <subha@i...> on Thu, 6 Dec 2001 19:03:32 +0530
Hi Calwin,



I got y'r pbm and got soln also for that.



1. Mistake you have done in y'r code :

   -----------------------------------

since you've created the objects by the product name, you should refer the

object

in "javascript" as product name only. but you refer it by the variable

"vars2" & "var3". see the below:

----------------------------------------------------------

(Document.f1.vars2.value)+cint(Document.f1.vars3.value) 

----------------------------------------------------------



since the object "vars2" is not available in the form, it is giving error

for you...

so do the following :



Solution:

--------



 1. Create the object like the below :



		i = 1

		j = 1

		while not rs.EOF 

			response.write("input type="text"

name="Product"&i&"b1"")

			response.write("input type="text"

name="Product"&i&"b2"")

			response.write("input type="text"

name="Product"&i&"b3"")

			i = i + 1

		end while

   so your objects have been created like 

	Product1b1,Product1b2,Product1b3 

	Product2b1,Product2b2,Product2b3 

	.... 

	Note : see the view source...



 2. Refer these objects in javascript like



	function add()

	{

		for(int i=1;i<=noofrows;i++)

		{

			val1

eval("document.formname."+"Product"+i+"b1.value")

			val2

eval("document.formname."+"Product"+i+"b2.value")

			val3 = val1 + val2

		

			eval("document.formname."+"Product"+i+"b3.value") 

val3

		}

	}



I think this will help you...



Bye

Subha




  Return to Index