/    Sign up×
Community /Pin to ProfileBookmark

need help w/js

Hi, I could use some help with my JS code. I’m sending my completed Captcha form to my email addy. There are several dropdowns, among which are 2 input fields “unitprices” and 2 “quantities”. Selecting 1 or both produces a “result(2)”. This works. It should produce “subtotal” and gtotal” but doesn’t. First below is the printout of the email sent. Following that is my code. Thanks in advnce.
=======================================================


[QUOTE]

taxrate = 0.00
unitprice1 = 249.95
qty1 = 1
result1 = 249.95
unitprice2 = 327.95
qty2 = 2
result2 = 655.90
subtotal =
total =
Name = chaz
FromEmailAddress = [email][email protected][/email]
x = 37

y = 8

[/QUOTE]

=================================================

[code=html]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>

</HEAD>
<BODY bgColor=#FFCC66><center>

<form name=”form1″ action=”http://www.SnapHost.com/captcha/send.aspx” id=”ContactUsCaptchaWebForm” method=”post”>
<input name=”skip_WhereToSend” type=”hidden” value=”[email protected]”>
<input name=”skip_SnapHostID” type=”hidden” value=”FYDXPGEUE96F”>
<input name=”skip_WhereToReturn” type=”hidden” value=”http://www.ckdoublenecks.biz/ckorder-thanks.html”>
<input name=”skip_Subject” type=”hidden” value=”My Order Form”>
<input name=”skip_ShowUsersIp” type=”hidden” value=”1″>
<input name=”skip_SendCopyToUser” type=”hidden” value=”1″>
<input type=”hidden” name=”taxrate” value=”0.00″> <!– 0% sales tax, adjust accordingly –>
<hr>

<table>
<tr>
<th>Product – Unit Price</th>
<th>Qty</th>
<th>Item total</th>
<tr>
<td align=”middle”><select name=”unitprice1″ onchange=”calculate(1)”>
<OPTION class=highlight value=”” selected>Select</OPTION>
<OPTION class=highlight value=”249.95″>Violina_conversion – 249.95</OPTION>
<OPTION class=highlight value=”327.95″>Pipsqueek_conversion – 327.95</OPTION>

</select></td>
<td align=”middle”><select name=”qty1″ onchange=”calculate(1)”>
<OPTION class=highlight value=”” selected>choose</option>
<OPTION class=highlight value=”1″>1</option>
<OPTION class=highlight value=”2″>2</option>

</select></td>
<td align=”middle”>$<input type=”text” class=”result” name=”result1″></td>
<tr>
<td align=”middle”><select name=”unitprice2″ onchange=”calculate(2)”>
<OPTION class=highlight value=”” selected>Select</OPTION>
<OPTION class=highlight value=”249.95″>Violina_conversion – 249.95</OPTION>
<OPTION class=highlight value=”327.95″>Pipsqueek_conversion – 327.95</OPTION>

</SELECT></td>
<td align=”middle”><select name=”qty2″ onchange=”calculate(2)”>
<OPTION class=highlight value=”” selected>choose</option>
<OPTION class=highlight value=”1″>1</option>
<OPTION class=highlight value=”2″>2</option>

</SELECT></td>
<td align=”middle” align=”middle”>$<input type=”text” class=”result” name=”result2″></td>
<tr>
<td align=”middle” colspan=3 class=”totals”>Subtotal: $<input type=”text” name=”subtotal”></td>
<tr><td colspan=3 class=”totals”>Grand Total: $<input type=”text” name=”gtotal”></td>
<tr>
<td><i>Name<font color=”red”>*</font></i>: <input type=”text” name=”Name” required></td>
<td><i>E-mail<font color=”red”>*</font></i>: <input type=”email” name=”FromEmailAddress” required></td>
<tr>
<td><i>Enter web form code<font color=”red”>*</font></i>:</td>
<tr>
<td>
<IMG src=”shakydigit.gif”><font color=red>**</font>
<input name=”skip_CaptchaCode” type=”text” maxlength=”6″ required></td>
<tr>
<td colspan=”2″>
<a href=”http://www.snaphost.com/captcha/ReadyForms/ContactUsForm.aspx”>
<img id=”CaptchaImage” alt=”Contact Us form” title=”HTML code for Contact Us form” src=”http://www.SnapHost.com/captcha/CaptchaImage.aspx?id=FYDXPGEUE96F&ImgType=2″></a><p>
<span onclick=”return ReloadCaptchaImage(‘CaptchaImage’);”>reload image</span></td>
<tr>
<td colspan=”3″>
<!– Please check our ProCaptcha service which is ad-free:
[url]http://www.SnapHost.com/captcha/ProCaptchaOverview.aspx[/url] –>
<a href=”http://www.snaphost.com/captcha/ProCaptchaOverview.aspx”>
web forms html</a><br>

<CENTER><INPUT type=image height=24 alt=”submit button” width=129
src=”rollsubmit.gif”>

<center><small>copyright &copy 2000-2016</small><BR>[/code]

[CODE]<script>
function calculate(n) {
var p = document.getElementsByName(‘unitprice’+n)[0].value,
q = document.getElementsByName(‘qty’+n)[0].value;
document.getElementsByName(‘result’+n)[0].value = (p * q).toFixed(2);
total();
}

function total(){
var s1 = document.querySelectorAll(‘.result’),
st = form1.subtotal.value;
st = 0;
for (var i = 0; i < s1.length; i++) {
var a = s1[i].value,
st = 1*a+1*st;
}
form.subtotal.value = st.toFixed(2);
form.tax.value = (st * form.taxrate.value).toFixed(2);
form.total.value = (st*1 + form.tax.value*1).toFixed(2);

}

function ReloadCaptchaImage(captchaImageId) {
var obj = document.getElementById(captchaImageId);
var src = obj.src;
var date = new Date();
var pos = src.indexOf(‘&rad=’);
if (pos >= 0) { src = src.substr(0, pos); }
obj.src = src + ‘&rad=’ + date.getTime();
return false;
}
</script>[/CODE]

[code=html]</form></CENTER></BODY></HTML>[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoMar 19.2016 — [CODE]
form.subtotal.value = st.toFixed(2);
form.tax.value = (st * form.taxrate.value).toFixed(2);
form.total.value = (st*1 + form.tax.value*1).toFixed(2);
[/CODE]


Where is form defined? Are there any errors in the console?
Copy linkTweet thisAlerts:
@rootMar 19.2016 — accessing forms ...

theValue = document.forms.formName.inputName.value;

you can access form elements by various other means, like through calling a function after an event is triggered such as when you move focus or if a change occurs...

I would remove[B] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">[/B]

put in [B]<!DOCTYPE html>[/B]

because HTML5 is supported by all the majors (last time I checked most features are now implemented) and use HTML5 form inputs, if HTML5 isn't supported then you will find that the form inputs will default to HTML4 and the form fields will be text input fields, so adding a check at the server on what the browser is, you can easily add in the additional JavaScript or choose to output a notice in addition to supplying support for older browsers.

If you are producing a shopping cart in JavaScript, it poses a number of problems, it is better that you use JavaScript + AJAX and submit the purchases to the server-side and store them in a database that holds the cart/basket information until the purchases are completed.

I really don't understand why people put faith in JavaScript carts / baskets, fine if you pushing data to a server, not fine if you're relying on JavaScript to control your final totals to obtain payment through another domain like paypal or worldpay for example or even through your own domain and bank... Not secure

Your cart would only need supply a rolled up value if you use AJAX / JavaScript --> server-side for non HTML5,

if HTML5, you would only need AJAX and HTML5 form fields --> server-side.

then your final page when the basket / cart totals are asked for can then be supplied and your payment form containing the token that points to your basket thats being paid for...
×

Success!

Help @12Strings spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...