|
 |
asp_web_howto thread: How to Dynamically Generate Javascript
Message #1 by "David Yee" <david@r...> on Sun, 23 Dec 2001 16:07:47 +0800
|
|
I have some problem with writing dynamic javascript in
ASP to calculate the total price of product selected..
Using ASP, i retrieved the product and pricing from the database
and display the list of product with a checkbox next to every
product.
At the bottom of the form i have a textbox to reflect the total
price of all selected product. May i know how can i do this?
I have a onclick event called calculate() for every checkbox
And this calculate function should be able to check on all the
checkbox and if it is checked then add the price to the total.
But i am confuse on how to use ASP to write the javascript
dynamically. Kindly advice me on this.
Thank You
<%
If Not ServiceRS.EOF Then
i = 1
While Not ServiceRS.EOF
Response.Write
"<tr>" & _
" <td><input type=checkbox value=""" & ServiceRS("CatID") & """
name=Interest" & i & " checked onclick=calculate()></td>" & _
" <td><font face=Verdana, Arial, Helvetica, sans-serif size=2>" &
ServiceRS("Catname") & "</font></td>" & _
" <td>$<input type=text size=8 name=price" & i & " value=" &
ServiceRS("Price") & " disabled></td>" & _
"</tr>" & VbCr & VbTab & VbTab
i = i + 1
ServiceRS.MoveNext
Wend
Response.Write
"<tr>" & _
" <td> </td>" & _
" <td><font face=Verdana, Arial, Helvetica, sans-serif
size=2>TOTAL : </font></td>" & _
" <td>$<input type=text size=8 name=totalprice value=" & numtotal
& " disabled></td>" & _
"</tr>"
End If
%>
Message #2 by "jparlato" <jparlato@m...> on Sun, 23 Dec 2001 09:32:17 -0500
|
|
You don't need to write the java script dynamically in your asp. You simply
use java script to
look at the check boxes and do the calculations. This code would be in your
calculate
routine. Using the dom, you can access the value of each check box using an
eval statement like this:
fltDollars = fltDollars + eval("document.forms[0]." + "fieldname" +
".value")
You can hard code the fieldname or use some naming convention like
"checkbox" + intCheckboxNumber
to create names dynamically fo each list box.
-----Original Message-----
From: David Yee [mailto:david@r...]
Sent: Sunday, December 23, 2001 3:08 AM
To: ASP Web HowTo
Subject: [asp_web_howto] How to Dynamically Generate Javascript
I have some problem with writing dynamic javascript in
ASP to calculate the total price of product selected..
Using ASP, i retrieved the product and pricing from the database
and display the list of product with a checkbox next to every
product.
At the bottom of the form i have a textbox to reflect the total
price of all selected product. May i know how can i do this?
I have a onclick event called calculate() for every checkbox
And this calculate function should be able to check on all the
checkbox and if it is checked then add the price to the total.
But i am confuse on how to use ASP to write the javascript
dynamically. Kindly advice me on this.
Thank You
<%
If Not ServiceRS.EOF Then
i = 1
While Not ServiceRS.EOF
Response.Write
"<tr>" & _
" <td><input type=checkbox value=""" & ServiceRS("CatID") & """
name=Interest" & i & " checked onclick=calculate()></td>" & _
" <td><font face=Verdana, Arial, Helvetica, sans-serif size=2>" &
ServiceRS("Catname") & "</font></td>" & _
" <td>$<input type=text size=8 name=price" & i & " value=" &
ServiceRS("Price") & " disabled></td>" & _
"</tr>" & VbCr & VbTab & VbTab
i = i + 1
ServiceRS.MoveNext
Wend
Response.Write
"<tr>" & _
" <td> </td>" & _
" <td><font face=Verdana, Arial, Helvetica, sans-serif
size=2>TOTAL : </font></td>" & _
" <td>$<input type=text size=8 name=totalprice value=" & numtotal
& " disabled></td>" & _
"</tr>"
End If
%>
$subst('Email.Unsub').
Message #3 by Oleg Kapeljushnik <c-oleg.kapeljushnik@w...> on Wed, 26 Dec 2001 10:33:25 -0500
|
|
Hi !!
To write ASP variables into JavaScript you can use this scenario :
<SCRIPT>
var abc=<%=ASPVariable%>
</SCRIPT>
Hope it will help you.
Oleg
-----Original Message-----
From: David Yee [mailto:david@r...]
Sent: December 23, 2001 3:08 AM
To: ASP Web HowTo
Subject: [asp_web_howto] How to Dynamically Generate Javascript
I have some problem with writing dynamic javascript in
ASP to calculate the total price of product selected..
Using ASP, i retrieved the product and pricing from the database
and display the list of product with a checkbox next to every
product.
At the bottom of the form i have a textbox to reflect the total
price of all selected product. May i know how can i do this?
I have a onclick event called calculate() for every checkbox
And this calculate function should be able to check on all the
checkbox and if it is checked then add the price to the total.
But i am confuse on how to use ASP to write the javascript
dynamically. Kindly advice me on this.
Thank You
<%
If Not ServiceRS.EOF Then
i = 1
While Not ServiceRS.EOF
Response.Write
"<tr>" & _
" <td><input type=checkbox value=""" & ServiceRS("CatID") & """
name=Interest" & i & " checked onclick=calculate()></td>" & _
" <td><font face=Verdana, Arial, Helvetica, sans-serif size=2>" &
ServiceRS("Catname") & "</font></td>" & _
" <td>$<input type=text size=8 name=price" & i & " value=" &
ServiceRS("Price") & " disabled></td>" & _
"</tr>" & VbCr & VbTab & VbTab
i = i + 1
ServiceRS.MoveNext
Wend
Response.Write
"<tr>" & _
" <td> </td>" & _
" <td><font face=Verdana, Arial, Helvetica, sans-serif
size=2>TOTAL : </font></td>" & _
" <td>$<input type=text size=8 name=totalprice value=" & numtotal
& " disabled></td>" & _
"</tr>"
End If
%>
$subst('Email.Unsub').
Message #4 by "Drew, Ron" <RDrew@B...> on Wed, 26 Dec 2001 11:02:42 -0500
|
|
You can either put the JavaScript code within the form itself or execute
a function using the onclick. But either way, remember it executes on
the client side not the server side. If you have more than one
calculation (more than one checkbox), use the this.value and execute a
function (easier coding to read and debug)...Here is an example of
calculating how much calories you use when running. I would be equal
to my age ugh!!
<HEAD>
<SCRIPT LANGUAGE=3D"JavaScript">
<!-- Begin
var myWeight;
var myDistance;
function HowMany(form) {
var difference;
difference =3D (myDistance * myWeight) * .653;
form.Fdiff.value =3D difference;
if (difference < 100) {
form.comment.value=3D"You better start working!";
}
if (difference > 101 && difference < 200) {
form.comment.value=3D"Nice run, but you can do better.";
}
if (difference > 201 && difference < 300) {
form.comment.value=3D"Very good! Push above 300 next time.";
}
if (difference > 301 && difference < 500) {
form.comment.value=3D"Great! Your a runner.....keep it up!";
}
if (difference > 501 && difference < 700) {
form.comment.value=3D"Bill Rogers move over!";
}
if (difference > 701) {
form.comment.value=3D"Your my hero! Have a jelly doughnut.";
}
}
function SetMyWeight(weight) {
myWeight =3D weight.value;
}
function SetmyDistance(dis) {
myDistance =3D dis.value;
}
function ClearForm(form){
form.myWeight.value =3D "";
form.myDistance.value =3D "";
form.Fdiff.value =3D "";
form.comment.value =3D "";
}
// End -->
</SCRIPT>
<BODY>
<CENTER>
<FORM METHOD=3D"POST">
<TABLE border=3D3>
<TR>
<TR>
<TD><div align=3Dcenter>Your<br>Weight</div></TD>
<TD><div align=3Dcenter>Miles<br>run</div></TD>
<TD><div align=3Dcenter>Calories<br>burned</div></TD>
<TD><INPUT TYPE=3DBUTTON ONCLICK=3D"HowMany(this.form)"
VALUE=3D"Calculate"></TD>
</TR>
<tr>
<TD><div align=3Dcenter><INPUT TYPE=3Dtext NAME=3DmyWeight
SIZE=3D"4"ONCHANGE=3D"SetMyWeight(this)"></div></TD>
<TD><div align=3Dcenter><INPUT TYPE=3Dtext NAME=3DmyDistance
SIZE=3D"4"ONCHANGE=3D"SetmyDistance(this)"></div></TD>
<TD><div align=3Dcenter><INPUT TYPE=3Dtext NAME=3D"Fdiff" VALUE=3D""
SIZE=3D"6"></div></TD>
<TD><div align=3Dcenter><INPUT TYPE=3DBUTTON VALUE=3D" Reset "
onClick=3D"ClearForm(this.form)"></div></tr>
</table>
<table border=3D3>
<tr>
<TD><DIV ALIGN=3DCENTER>Comments</DIV></TD>
<TD><INPUT TYPE=3Dtext NAME=3D"comment" size=3D"37"></td>
</TR>
</TABLE>
</FORM>
-----Original Message-----
From: Oleg Kapeljushnik [mailto:c-oleg.kapeljushnik@w...]
Sent: Wednesday, December 26, 2001 10:33 AM
To: ASP Web HowTo
Subject: [asp_web_howto] RE: How to Dynamically Generate Javascript
---
Need a present for your favorite programmer?
E-Documents from Amazon.com at http://p2p.wrox.com/edocs.asp
---
Hi !!
To write ASP variables into JavaScript you can use this scenario :
<SCRIPT>
var abc=3D<%=3DASPVariable%>
</SCRIPT>
Hope it will help you.
Oleg
-----Original Message-----
From: David Yee [mailto:david@r...]
Sent: December 23, 2001 3:08 AM
To: ASP Web HowTo
Subject: [asp_web_howto] How to Dynamically Generate Javascript
I have some problem with writing dynamic javascript in
ASP to calculate the total price of product selected..
Using ASP, i retrieved the product and pricing from the database and
display the list of product with a checkbox next to every product.
At the bottom of the form i have a textbox to reflect the total price of
all selected product. May i know how can i do this?
I have a onclick event called calculate() for every checkbox And this
calculate function should be able to check on all the checkbox and if it
is checked then add the price to the total.
But i am confuse on how to use ASP to write the javascript dynamically.
Kindly advice me on this.
Thank You
<%
If Not ServiceRS.EOF Then
i =3D 1
While Not ServiceRS.EOF
Response.Write
"<tr>" & _
" <td><input type=3Dcheckbox value=3D""" & ServiceRS("CatID") &
"""
name=3DInterest" & i & " checked onclick=3Dcalculate()></td>" & _
" <td><font face=3DVerdana, Arial, Helvetica, sans-serif
size=3D2>" &
ServiceRS("Catname") & "</font></td>" & _
" <td>$<input type=3Dtext size=3D8 name=3Dprice" & i & "
value=3D" &
ServiceRS("Price") & " disabled></td>" & _
"</tr>" & VbCr & VbTab & VbTab
i =3D i + 1
ServiceRS.MoveNext
Wend
Response.Write
"<tr>" & _
" <td> </td>" & _
" <td><font face=3DVerdana, Arial, Helvetica, sans-serif
size=3D2>TOTAL : </font></td>" & _
" <td>$<input type=3Dtext size=3D8 name=3Dtotalprice value=3D"
&
numtotal
& " disabled></td>" & _
"</tr>"
End If
%>
---
Change your mail options at http://p2p.wrox.com/manager.asp or to
unsubscribe send a blank email to
$subst('Email.Unsub').
$subst('Email.Unsub').
|
|
 |