ok, so i've my front end shop code almost working, the quantity and price all add up........ now i need to add a curency conversion function, euro (default), dollar, and uk pound.
right, i'm not sure if i should have 3 seperate functions, that do the math on the total box, and add the required, €, £ or $ icon....... it would be best to put this in one function.
ok, here's my first atempt, getting a NaN result...... i've set the values in the radio buttons.......
here's the code, hopefully you can see where i'm going wrong, its the "convert" function -
-
<script language="javascript1.2">
-
-
window.onload = addnumbers;
-
window.onload = convert;
-
-
function convert()
-
{
-
-
var l, t;
-
-
t=0;
-
-
for (l=0; l<document.form1.currency.length;l++)
-
{
-
if (document.form1.currency[l].checked)
-
{
-
t = parseInt(document.form1.currency[l].value) * parseInt(document.form1.totalbox.value);
-
}
-
}
-
-
document.form1.totalbox.value = t;
-
}
-
-
-
function checkreset()
-
{
-
-
if (confirm('Are you sure you wish to clear the form?'))
-
{
-
return true
-
}
-
return false
-
}
-
-
//function addshipping()
-
-
//{
-
-
//}
-
-
function addnumbers()
-
{
-
-
var a, b, c, d, e, f, g, h, v, j, s, i;
-
-
// This function reads the values from the input boxes
-
// parseInt converts the input boxes into an integer
-
a = parseInt(document.form1.abox.value);
-
b = parseInt(document.form1.bbox.value);
-
c = parseInt(document.form1.cbox.value);
-
d = parseInt(document.form1.dbox.value);
-
e = parseInt(document.form1.ebox.value);
-
f = parseInt(document.form1.fbox.value);
-
g = parseInt(document.form1.gbox.value);
-
h = parseInt(document.form1.hbox.value);
-
v = parseInt(document.form1.ibox.value);
-
j = parseInt(document.form1.jbox.value);
-
-
-
//sets default value of shipping to 0, then
-
//loops through the shipping radio boxes to get value
-
-
s=0;
-
-
-
for (i=0; i<document.form1.shipping.length;i++)
-
{
-
if (document.form1.shipping[i].checked)
-
{
-
s = parseInt(document.form1.shipping[i].value);
-
}
-
}
-
-
-
// Then we do the math
-
result = a + b + c + d + e + f + g + h + v + j;
-
-
-
// This adds the total number of products to the quantity box
-
document.form1.qbox.value = result;
-
-
// This calculates the total price, including vat at 21% and adds the shipping cost
-
// toFixed fixes the decimal point 2 places
-
-
-
document.form1.totalbox.value = "€" + (s+result*10*21/100+result*10).toFixed(2);
-
-
-
-
-
-
// As the function has already done the output, we don’t need a return value
-
}
-
</script>
-
-
9 2416
Use the default (euro) as the standard, i.e. 1.00 and then get the currency rates for the pound and the dollar based on the euro.
Note that this should not really be done using JavaScript because the values change all the time. You should be using server-side code to get real-time rates.
However, if using as an estimate it may suffice as long as you update it often enough or, better still, get the data while loading the page (using server-side code).
t's just a project, so i can have fixed rates. i have the rates for euro and the dollar.
the default price apears in a text box in euros.
i have this function i'm using to convert that value, but it's not working -
function convert()
-
{
-
-
var l, t;
-
-
t=0;
-
-
for (l=0; l<document.form1.currency.length;l++)
-
{
-
if (document.form1.currency[l].checked)
-
{
-
t = parseInt(document.form1.currency[l].value) * parseInt(document.form1.totalbox.value);
-
}
-
}
-
-
document.form1.totalbox.value = t;
-
}
-
and the relevant html code -
<table width="100%" border="0" cellpadding="6" cellspacing="1" bgcolor="#FFF7D2">
-
<tr>
-
<td colspan="3" bgcolor="#FFF7D2"><span class="style3">Please choose your currency, default is in euros. </span></td>
-
</tr>
-
<tr>
-
<td width="33%" align="center" bgcolor="#FFFFFF"><span class="style1"><strong>Euro</strong> </span><br />
-
<input name="currency" type="radio" onclick="convert()" value="0" /></td>
-
<td width="33%" align="center" bgcolor="#FFFFFF"><span class="style1"><strong>US Dollar</strong><br />
-
</span>
-
<input type="radio" name="currency" onclick="convert()" value=".69" /></td>
-
<td width="33%" align="center" bgcolor="#FFFFFF"><span class="style1"><strong>UK Pound </strong><br />
-
</span>
-
<input type="radio" name="currency" onclick="convert()" value="1.44" /></td>
-
</tr>
-
</table>
-
Use 1 instead of 0 for the euro value and use parseFloat instead of parseInt because you will be dealing with non-integer values.
thats still not working, it has to be something wrong with this code, i'm pretty new to this so, what i'm trying to do is get the value of the total box, multiply it by the cosen numerical value on the currency radio buttons.
code - function convert()
-
{
-
-
var l, t;
-
-
t=0;
-
-
for (l=0; l<document.form1.currency.length;l++)
-
{
-
if (document.form1.currency[l].checked)
-
{
-
t = parseInt(document.form1.currency[l].value) * parseInt(document.form1.totalbox.value);
-
}
-
}
-
-
document.form1.totalbox.value = t;
-
}
You've not changed parseInt to parseFloat.
When you say it's not working, what does it mean? Errors or nothing?
You've not changed parseInt to parseFloat.
When you say it's not working, what does it mean? Errors or nothing?
sorry, changed that, parseFloat does the same thing.
what happens is... the text box, called totalbox, holds the value for the quantity of items picked, including the shipping cost.
when you select one of the radio buttons, ie the dollar one, it turns the amount in the total box to NaN, i'm asuming this line is causing the problem... - t = parseFloat(document.form1.totalbox.value) * parseFloat(document.form1.currency[l].value);
Change the value of the dollar radio button to 0.69.
Another thing you should do is use the calculated total value, not the value in the totals box.
Change the value of the dollar radio button to 0.69.
Another thing you should do is use the calculated total value, not the value in the totals box.
i've changed to 0.69, still the same.
i dont really understand your second point, could you clarify please.
the total value is the value that apears in the totals box.... - document.form1.totalbox.value = "€" + (s+result*10*21/100+result*10).toFixed(2);
i dont really understand your second point, could you clarify please.
the total value is the value that apears in the totals box...
It is, but when you click on a radio button and it changes to another currency and then decide to change to another currency, the changed currency total value is used to multiply with the currency rate giving incorrect results.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
reply
views
Thread by Marcelo López |
last post: by
|
2 posts
views
Thread by Willing 2 Learn |
last post: by
|
3 posts
views
Thread by Dalan |
last post: by
|
reply
views
Thread by R. John Reed |
last post: by
|
7 posts
views
Thread by meenasamy |
last post: by
|
14 posts
views
Thread by Scott M. |
last post: by
| | |
25 posts
views
Thread by mereba |
last post: by
| | | | | | | | | | |