Forum Moderators: open

Message Too Old, No Replies

Multiplying Variable

Any help appreciated, i'm sure it's simple, but I'm even simpler

         

mangotude

8:38 pm on Mar 22, 2005 (gmt 0)

10+ Year Member



Hello,

I have what is I think a straightforward problem, but I'm pretty dyslexic with programming, and as such find it near impossible to create individual code from general examples. I have tried to code from scratch, but it's so flawed, I'm at a loss.

My current procedure is this:

I have a site where the customer fills out a form (there is an array which assigns a price according to choice of item and country).

on completing the form, it is written to a database, the basics of the order is displayed, and then the options to go onto payment.

basic display is


<b>Order No </b> <<ID>><br>
<b>Item </b> <<description>><br>
<b>Price </b>&pound;<<price>><br>

This all works without any problems.

On the same page, I want to have a separate payment option, which would be identical, except the currency would be in dollars.

So what I want to do is take the variable 'price' and create another (say, 'price2') which is would be the price in USD.

I would like it to be something like (i know this isnt proper code)

price2=price*x

and I could just go into the html of my page and alter 'x' every week (or day, with the current volatility)

If possible, I really don't want to alter the database structure, especially as this is a trial period.

I apologise for such a lengthy message for a straightforward problem, but I guess the more information the better.

Any advice greatly appreciated.

NB - the quotation box has put extra line break.

SpaceFrog

8:43 am on Mar 23, 2005 (gmt 0)

10+ Year Member



you could have an external js file with increase coef.

just put:

var coef=1.5
function pricing(){
TabPrices=document.getElementsByName('Price')
for (i=0;i<TabPrices.length;i++){
TabPrices[i].value*=coef;
}
}

in a text file and call it increase.js

then in your html page head:

<script type='text/javascript' src="increase.js"></script>

in your page:

<body onload="pricing()">

item1 <input type="text" readonly name="Price" value=2 /><br/>
item2<input type="text" readonly name="Price" value=4 /><br/>
item3<input type="text" readonly name="Price" value=8 /><br/>

</html>

do not forget to put noscript tags in your page as if js is not activated prices would remain as value in input ...

you then only need to change coef in je file and all your pages will be updated in price ...

mangotude

2:16 pm on Mar 23, 2005 (gmt 0)

10+ Year Member



Hi,

Thanks for that. I'll give it a try and let you know how it goes. Just one thing - I'm wanting for both prices to be on the page (one for GBP and one for USD) - would this add an additional price - e.g. 'price2' or rather would it replace the existing 'price'?

SpaceFrog

3:07 pm on Mar 23, 2005 (gmt 0)

10+ Year Member



what this script does is that it modifies existing price in input text
but you can modify it to add converted price in what ever currency you want ...

SpaceFrog

3:14 pm on Mar 23, 2005 (gmt 0)

10+ Year Member




<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Exchange rate</title>

<script type='text/javascript'>
var coef=1.57
function pricing(){
TabPrices=document.getElementsByName('Price')
for (i=0;i<TabPrices.length;i++){
document.getElementsByName(TabPrices[i].name+'GBP')[i].value=TabPrices[i].value*coef;
}
}
</script>

</head>

<body onload="pricing()">

item1 USD<input type="text" readonly name="Price" value=2 />GBP<input type="text" readonly name="PriceGBP" value=2 /><br/>
item2 USD<input type="text" readonly name="Price" value=4 />GBP<input type="text" readonly name="PriceGBP" value=2 /><br/>
item3 USD<input type="text" readonly name="Price" value=8 />GBP<input type="text" readonly name="PriceGBP" value=2 /><br/>
<br/>

</body>

</html>

mangotude

10:29 pm on Mar 23, 2005 (gmt 0)

10+ Year Member



Hi, I've managed to implement it to get Halfway with it.

In head


<script type='text/javascript'>
var coef=1.57
function pricing(){
TabPrices=document.getElementsByName('Price')
for (i=0;i<TabPrices.length;i++){
document.getElementsByName(TabPrices[i].name+'GBP')[i].value=TabPrices[i].value*coef;
}
}
</script>

in body


<body onload="pricing()">

My original form value was defined as 'price' so I have used in body the following code


<<input type="hidden" name="Price" value="<<price>>" />
<input type="readonly" name="PriceGBP" /><br/>

so the usual price carried over would be 25.00, so this is displaying in the box 39.25.

So far, so good.

So now I want to use this new calculation for two things.

for my existing information, i recap the order as such


<b>Order No </b> <<ID>><br>
<b>Item </b> <<description>><br>
<b>Price<br> <<price>><br>

and in when sending my form to payment processor, i use the following.


<form action https://www.zzzzzz.com/yyyy" method="post">
...
<input type="hidden" name="amount" value="<<price>>">
...
</form>

These currently work fine. So I want essentially duplicates of these for the new price.

But I've currently been unable to carry it over - If I replace Price with PriceGBP or other variations, all is blank.

Thanks for the help so far.

SpaceFrog

7:39 am on Mar 24, 2005 (gmt 0)

10+ Year Member



Sorry I didn't follow you there ...

I understand that so far you have managed to implement the code I gave you to shox converted prices ...

What is the next step with the hidden inputs?

mangotude

10:31 am on Mar 24, 2005 (gmt 0)

10+ Year Member



It may not be a javascript issue, but I want to be able to take 'PriceGBP' and

1> display it in the order recap

2> be able to have it passed to my payment processor through a form

SpaceFrog

10:37 am on Mar 24, 2005 (gmt 0)

10+ Year Member



do you mean that the input GBP are not forwarded with other form elements?

mangotude

11:19 am on Mar 24, 2005 (gmt 0)

10+ Year Member



Hi,

I suppose its an issue of forwarding - if I'm trying to use the recap


<b>Order No </b> <<ID>><br>
<b>Item </b> <<description>><br>
<b>Price<br> <<price>><br>

and the form elements


<form action https://www.zzzzzz.com/yyyy" method="post">
...
<input type="hidden" name="amount" value="<<price>>">
...
</form>

I am unable to get the new variable to appear in something like this (using, for example,

<<PriceGBP>>
)

SpaceFrog

12:56 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



sorry i have never encountered double << >> notation before ...
I really do not know what that does ...

I guess your sublit passes thru some kind of recap ...

mangotude

2:16 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



Hi

the <<>> is what I used to get to display the fields - It probably has nothing to do with Javascript, but it works on the existing details - whether I have to permanently define the PriceGBP I don't know. I would just like to be able to show it in that manner and also have it passed in a form.

thanks for your help in getting the formula and the calcuations set up, was greatly appreciated.

James