...

View Full Version : Please help me control forms



sAuhsoj
07-12-2011, 08:34 AM
I am trying to set up a form for paypal where the amount (cost) of the product is based on the currency chosen. I am trying to set this up using javascript which would submit the form and change the amount in the form. However, as a beginner, I am having some issues.
This is what I have currently:
<html>
<head>
<script type="text/javascript">
function CalculateOrder(theform)
{
var currency = theform.currency_code.value;
if (currency == "GBP")
{
theform.amount.value = "1.29"
}
else if (currency == "USD")
{
theform.amount.value = "1.99"
}
else if (currency == "EUR")
{
theform.amount.value = "1.39"
}
document.payform.submit();
}
</script>
</head>
<body>

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr " method="post" name="payform">
<input type="hidden" name="on0" value="Currency">
<select name="currency_code">
<option value="GBP" selected >GBP</option>
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
</p>
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif " onClick="CalculateOrder(this.form)" alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="add" value="1">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="myemail@gmail.com">
<input type="hidden" name="item_name" value="My Prouct">
<input type="hidden" name="amount" value="0.00">
<input type="hidden" name="shipping" value="0">
<input type="hidden" name="no_shipping" value="1">
</form>
</fieldset>
</body>
</html>The javascript is executed but clicking the image submits the form before the javascript runs and I am worried that the javascript may not run before the form is submitted so the amount would not be calculated - please help!
I do not want to calculate the costs based on the exchange rate. I would really prefer (and also find it simpler) to just specify the cost in each currency.
Thanks in advance!

devnull69
07-12-2011, 10:15 AM
You should move the execution of the function from the image onclick to the form onsubmit


<script type="text/javascript">
function CalculateOrder(theform)
{
var currency = theform.currency_code.value;
if (currency == "GBP")
{
theform.amount.value = "1.29"
}
else if (currency == "USD")
{
theform.amount.value = "1.99"
}
else if (currency == "EUR")
{
theform.amount.value = "1.39"
}
}
</script>

...

<form rel="nofollow" target="paypal" action="https://www.paypal.com/cgi-bin/webscr " method="post" name="payform" onsubmit="CalculateOrder(this)" >
<input type="hidden" name="on0" value="Currency">
<select name="currency_code">
<option value="GBP" selected >GBP</option>
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
</p>
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif " alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="add" value="1">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="myemail@gmail.com">
<input type="hidden" name="item_name" value="My Prouct">
<input type="hidden" name="amount" value="0.00">
<input type="hidden" name="shipping" value="0">
<input type="hidden" name="no_shipping" value="1">
</form>

sAuhsoj
07-12-2011, 11:29 AM
Thanks.
Why does clicking the image submit the form in the first place?
Is this all I need to make it work?

devnull69
07-12-2011, 11:59 AM
An input type="image" essentially is a submit button. Clicking it will submit the form by default.

sAuhsoj
07-12-2011, 12:26 PM
Okay, thanks a lot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum