Newbie: Javascript to calculate between currencies?

D

dysfunctional

Hello,
Hopefully some of you will be kind enough to help a Javascript zero like me
out.

I run a CGI-based hotel search online, which can be found at
http://www.triotours.com/faq/ma/hotel-search.htm

Part of the search is a piece of Javascript that someone wrote for me. It
does two things:

1. It sets the maximum rate ("max") that a visitor wants to search the
database with;
2. It sets which column in the database to search for that "max": either the
column "single_from" or the column "double_from".

The code of this Javascript:

function checkForm(f)
{ if ((!f.min.value)¦¦(!f.max.value)) {return true};
var roomtype;
for (i=0;i<f.elements.length;i++) {
e=f.elements;
if ((e.name=="rmtype")&&(e.checked)) {
roomtype=e.value;
}
}
if (roomtype=="Single") {
f.double_from.value="";
f.single_from.value="between "+f.min.value+" and "+f.max.value;
} else {
f.single_from.value="";
f.double_from.value="between "+f.min.value+" and "+f.max.value;
} return true;
}


One of the flaws of the form is that it requires "max" to be entered in
Moroccan dirham, which is obviously a currency that not many people are
familiar with.

So, I'd like to add a select list name="currency", the value of which would
then be used to calculate the input into dirhams.


<select name="currency">
<option value="1">Moroccan dirham</option>
<option value="9">US dollar</option>
<option value="11">Euro</option>
<option value="0.1">Japanese yen</option>
....etcetera


The value in the options is the conversion rate to Moroccan dirham. I chose
that because I thought it would be clever to work around a series of "var"
statements.

And that's where my knowledge of Javascript ends. I've played around with
document.getElementById and such, but I can't for the life of me find out
how I can get the value from "currency" into the Javascript code, so that it
does:

max = max * currency

If someone out there can, I'd be very grateful!

Jan
 
T

Touffy

So, I'd like to add a select list name="currency", the value of which would
then be used to calculate the input into dirhams.


<select name="currency">
<option value="1">Moroccan dirham</option>
<option value="9">US dollar</option>
<option value="11">Euro</option>
<option value="0.1">Japanese yen</option>
...etcetera


The value in the options is the conversion rate to Moroccan dirham. I chose
that because I thought it would be clever to work around a series of "var"
statements.

that's clever indeed.
And that's where my knowledge of Javascript ends. I've played around with
document.getElementById and such, but I can't for the life of me find out
how I can get the value from "currency" into the Javascript code, so that it
does:

max = max * currency

you can refrain from writing "max *= currency" ?! I wish I could (or do I ?)
sorry... anyway, back to your problem...

you can unambiguously grab a reference to your select element with this code :

currencyMenu = document.forms.f.currency

then you can get the currently selected option's value, which happens
to be copied into the select's own value property, and multiply :

max *= currencyMenu.value // sorry... can't help

Now let's put those two lines into your friend's function. I've also
added some tabs and linebreaks and removed some useless brackets. Code
needs to breathe.

function checkForm(f)
{
if( !f.min.value || !f.max.value )
return false;

var roomtype;
for( i=0; i<f.elements.length; i++ )
{
e=f.elements;
if( e.name=="rmtype" && e.checked )
roomtype=e.value;
}
// the above loop seems a lot of trouble to check 2 radio buttons
// but it really doesn't matter so I'll leave it alone.

// the max value is used below, so we need to do the maths now
var currencyMenu = document.forms.f.currency
max *= currencyMenu.value

if( roomtype=="Single" )
{
f.double_from.value="";
f.single_from.value="between "+f.min.value+" and "+f.max.value;
} else {
f.single_from.value="";
f.double_from.value="between "+f.min.value+" and "+f.max.value;
}

return true;
}


Voilà.
 
D

dysfunctional

Thanks for your time, David!

I'm afraid the script doesn't work, though. The form no longer returns a max
value. Instead it just displays all the results based on "city_id" (the
city), disregarding the input from "max"...


Touffy said:
So, I'd like to add a select list name="currency", the value of which would
then be used to calculate the input into dirhams.


<select name="currency">
<option value="1">Moroccan dirham</option>
<option value="9">US dollar</option>
<option value="11">Euro</option>
<option value="0.1">Japanese yen</option>
...etcetera


The value in the options is the conversion rate to Moroccan dirham. I chose
that because I thought it would be clever to work around a series of "var"
statements.

that's clever indeed.
And that's where my knowledge of Javascript ends. I've played around with
document.getElementById and such, but I can't for the life of me find out
how I can get the value from "currency" into the Javascript code, so that it
does:

max = max * currency

you can refrain from writing "max *= currency" ?! I wish I could (or do I ?)
sorry... anyway, back to your problem...

you can unambiguously grab a reference to your select element with this code :

currencyMenu = document.forms.f.currency

then you can get the currently selected option's value, which happens
to be copied into the select's own value property, and multiply :

max *= currencyMenu.value // sorry... can't help

Now let's put those two lines into your friend's function. I've also
added some tabs and linebreaks and removed some useless brackets. Code
needs to breathe.

function checkForm(f)
{
if( !f.min.value || !f.max.value )
return false;

var roomtype;
for( i=0; i<f.elements.length; i++ )
{
e=f.elements;
if( e.name=="rmtype" && e.checked )
roomtype=e.value;
}
// the above loop seems a lot of trouble to check 2 radio buttons
// but it really doesn't matter so I'll leave it alone.

// the max value is used below, so we need to do the maths now
var currencyMenu = document.forms.f.currency
max *= currencyMenu.value

if( roomtype=="Single" )
{
f.double_from.value="";
f.single_from.value="between "+f.min.value+" and "+f.max.value;
} else {
f.single_from.value="";
f.double_from.value="between "+f.min.value+" and "+f.max.value;
}

return true;
}


Voilà.
 
T

Touffy

Thanks for your time, David!

I'm afraid the script doesn't work, though. The form no longer returns a max
value. Instead it just displays all the results based on "city_id" (the
city), disregarding the input from "max"...

whoops sorry. The solution is so simple I just had to make an error and
never think to test the code. Here's the correct version :


function checkForm(f)
{
if( !f.min.value || !f.max.value )
return true;

var roomtype;
for( i=0; i<f.elements.length; i++ )
{
e=f.elements;
if( e.name=="rmtype" && e.checked )
roomtype=e.value;
}

f.max.value *= f.currency.value

if( roomtype=="Single" )
{
f.double_from.value="";
f.single_from.value="between "+f.min.value+" and "+f.max.value;
} else {
f.single_from.value="";
f.double_from.value="between "+f.min.value+" and "+f.max.value;
}

return true;
}
 
D

dysfunctional

OMG - You put me to complete shame. That IS simple.

Thank you so much for your time, and for a very valuable lesson!

Have a wonderful weekend,

Jan


Touffy said:
Thanks for your time, David!

I'm afraid the script doesn't work, though. The form no longer returns a max
value. Instead it just displays all the results based on "city_id" (the
city), disregarding the input from "max"...

whoops sorry. The solution is so simple I just had to make an error and
never think to test the code. Here's the correct version :


function checkForm(f)
{
if( !f.min.value || !f.max.value )
return true;

var roomtype;
for( i=0; i<f.elements.length; i++ )
{
e=f.elements;
if( e.name=="rmtype" && e.checked )
roomtype=e.value;
}

f.max.value *= f.currency.value

if( roomtype=="Single" )
{
f.double_from.value="";
f.single_from.value="between "+f.min.value+" and "+f.max.value;
} else {
f.single_from.value="";
f.double_from.value="between "+f.min.value+" and "+f.max.value;
}

return true;
}
 
D

dysfunctional

Just posting this for any future reference.

The solution David was so kind to give me had one downside. It changed the
value in the <input name="max"> field on the search page itself.

When a visitors submits the search, then uses the Back button to return from
the results page to the search form, the calculated value is visible in the
<input name="max"> field.

To avoid confusion, I have made the calculated value a new variable
"calcMax" inside the code, so it does not change the input field. The code
is below.

Thanks again to David for getting me on the right track!

-----
function checkForm(f)
{ if ((!f.min.value)||(!f.max.value)) {return true};
var roomtype;
for (i=0;i<f.elements.length;i++) {
e=f.elements;
if ((e.name=="rmtype")&&(e.checked)) {
roomtype=e.value;
}
}
var calcMax = (f.max.value)*(f.currency.value);
if (roomtype=="Single") {
f.double_from.value="";
f.single_from.value="between "+f.min.value+" and "+calcMax;
} else {
f.single_from.value="";
f.double_from.value="between "+f.min.value+" and "+calcMax;
} return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top