Click to See Complete Forum and Search --> : change Total to dollar amt and cent amt
cheryl
02-06-2003, 09:35 PM
Hi
I don't know javascript, I would like to find someone who can direct me . I have a script that totals an order form in cents to give a total as eg 4995. Then the script tells the total to display itself with a decimal point as 49.95. This is fine and it works well. However the bank's secure site wants the total passed to it as two separate amounts. A dollar amount and a cent amount. I need to know how to write the script that will take the total eg 49.95 ( or is it really 4995? ) and make it display in two input boxes as 49 and 95. Can anyone help me or direct me to a source of help? Please. Thank you in anticipation.
Cheryl
khalidali63
02-06-2003, 09:46 PM
The code snippet below should help you do that
var num = "49.95";
var number = num.split(".");
alert(number[0]+", "+number[1]);
cheers
Khalid
cheryl
02-06-2003, 11:15 PM
Thank you Khalid
The number in the beginning is not going to be 49.95 every time, that is just an example. The number is the result of the total of the form script that is totalling the orderform. So would I put in the first line
var num="total"; ?
Here is the script that I am working with:
var SAmt1 = 0.00,SAmt2 = 0.00,SAmt3= 0.00,SAmt4 = 0.00,SAmt5 = 0.00,SAmt6= 0.00,SAmt7= 0.00;
var STotal = 0.00;
var Item1 ="",Item2 = "",Item3 = "",Item4 ="",Item5 = "",Item6 = "",Item7 = "";
var SShipping = 0;
/*****************
** Add a decimal point to a number
*/
function AddDecimal(number) {
var withdecimal = "";
var num = "" + number;
if (num.length == 0) {
withdecimal += "0";
} else if (num.length == 1) {
withdecimal += "0.0" + num;
} else if (num.length == 2) {
withdecimal += "0." + num;
} else {
withdecimal += num.substring(0, num.length - 2);
withdecimal += "."
withdecimal += num.substring(num.length - 2, num.length);
}
return withdecimal;
}
function compute(form)
{
SAmt1 = Math.round(eval(form.Item1.value * 4556));
SAmt2 = Math.round(eval(form.Item2.value * 4570));
SAmt3 = Math.round(eval(form.Item3.value * 4605));
SAmt4 = Math.round(eval(form.Item4.value * 1933));
SAmt5 = Math.round(eval(form.Item5.value * 1842));
SAmt6 = Math.round(eval(form.Item6.value * 1842));
SAmt7 = Math.round(eval(form.Item7.value * 1895));
SShipping = 1000;
SShipping = Math.round(SShipping);
STotal =(SAmt1 + SAmt2 + SAmt3 + SAmt4 + SAmt5 + SAmt6 + SAmt7 + SShipping);
form.Amt1.value = AddDecimal(SAmt1);
form.Amt2.value = AddDecimal(SAmt2);
form.Amt3.value = AddDecimal(SAmt3);
form.Amt4.value = AddDecimal(SAmt4);
form.Amt5.value = AddDecimal(SAmt5);
form.Amt6.value = AddDecimal(SAmt6);
form.Amt7.value = AddDecimal(SAmt7);
form.Shipping.value = AddDecimal(SShipping);
form.Total.value = AddDecimal(STotal);
}
function ClearForm(form)
{form.Item1.value = "";
form.Item2.value = "";
form.Item3.value = "";
form.Item4.value = "";
form.Item5.value = "";
form.Item6.value = "";
form.Item7.value = "";
SShipping = 0;
form.Shipping.value = 0;
STotal = 0;
form.Total.value = 0;
SAmt1 = 0;
form.Amt1.value = 0;
SAmt2 = 0;
form.Amt2.value = 0;
SAmt3 = 0;
form.Amt3.value = 0;
SAmt4 = 0;
form.Amt4.value = 0;
SAmt5 = 0;
form.Amt5.value = 0;
SAmt6 = 0;
form.Amt6.value = 0;
SAmt7 = 0;
form.Amt7.value = 0;
SShipping = 0;
form.Shipping.value = 0
STotal = 0;
form.Total.value = 0;
}
-------------
the form has an input for the total that says:
<td colspan="2" align="center"><input type="button" value="Calculate Total" ONCLICK="compute(this.form)"> <input type="button" value="Clear Products Form" ONCLICK="ClearForm(this.form)"> </td>
<td align="center"><b>Total</b></td>
<td align="center">$<input type="text" name="Total" size="6"</td>
It is this Total that has to be passed to the bank in the following form:
<td><input type="text" name="amountDollar" size="6"></td>
<td align="center"><b>.</b></td>
<td><input type="text" name="amountCent" size="2"></td>
Will the snippet that you so kindly gave me do that?
Cheers Cheryl
khalidali63
02-06-2003, 11:26 PM
yes I knew 49.95 was just a number.
Please replace it with ayour varaible that holds the total in the sahpe of xx.xx
and then follow the script I posted.
cheers
Khalid
cheryl
02-06-2003, 11:47 PM
Forgive my lack of etiquette Khalid
I tried to put the snippet in and it came back with "STotal is undefined"
I made the first line
var num="STotal";
because I thought STotal was the variable. But maybe I don't really know what a variable is.
I get the feeling that you think I know a bit about this, but I don't.
I put the following after var SShipping = 0;
var num = "STotal";
var number = num.split(".");
alert (number[0]+","+number[1]);
It did not stop the order form from functionning but it didn't appear to do anything.
Cheers Cheryl
khalidali63
02-07-2003, 12:02 AM
in your code locat function computeForm(form)
in this function almost towards the end,find this line
form.Total.value = AddDecimal(STotal);
Now I take it you want to put dollars in one text box ans cents in another.
after the line above put the following code
var gTotal =( AddDecimal(STotal)).toString();
var number = gTotal .split(".");
alert(number[0]+", "+number[1]);
I hope this should clear some questions...
:-)
cheryl
02-07-2003, 12:19 AM
Thank you Khalid
That works very well to split the total into dollars and cents.
The bank won't accept an alert box though, the dollars have to go into the input box called 'amountDollar' and the cents have to go into an input box called 'amountCents' so that when the customer clicks on the "proceed with payment" button the 'amountDollar' and 'amountCents' can be picked up on the banks secure credit card payment page.
so would I write something like:
form.amountDollar.value = (number[0}+);
form.amountCents.value = (+number[1]);
instead of the 'alert' bit?
Cheers Cheryl
khalidali63
02-07-2003, 12:24 AM
almost...:)
I missed to type that part in
document.formName.amountDollar.value =number[0];
document.formName.amountCents.value = number[1];
where formName will be the name of the form you have.
cheers
Khalid
cheryl
02-07-2003, 12:56 AM
Thank you that works very well . Does the alert box have to pop up or is it possible to make the two separate amounts go straight into the boxes without having to click the alert OK.
cheers Cheryl
khalidali63
02-07-2003, 06:59 AM
nope..comment out the alert or delete the line that displays alert.
cheers
Khalid
cheryl
02-09-2003, 07:03 PM
Thank you for replying over the weekend. What a nice surprise to get back to work and find the answer waiting for me. Commenting out the alert line appears to work perfectly. Now the amount just pops into the dollar and cent boxes like a charm. You are an angel.
Cheers Cheryl
JDzines
02-16-2003, 12:04 PM
I was reading your thread and was wondering if I could see what your form looks like. I'm making a similar one and it may help me out. Do you have a link to it?
Thanks-
cheryl
02-16-2003, 06:32 PM
when the site goes up ( in the next couple of days hopefully )
the url of the form page will be http://www.clearwaters-herbary.com.au/secure_ordering.html
Help yourself to the code.
Cheers Cheryl
JDzines
02-16-2003, 10:53 PM
Thanks!
I'm looking forward to seeing it.