Jump to content

Javascript form help

- - - - -

  • Please log in to reply
8 replies to this topic

#1
sAuhsoj

sAuhsoj

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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!
Thanks in advance

#2
sAuhsoj

sAuhsoj

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
I change the thread as I realised I had made a massive error in this - that the image was not part of the form.

#3
sAuhsoj

sAuhsoj

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
any ideas? I would really like this ready soon

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I would use an AJAX call to request the conversion rate for a given denomination, and use a PHP script with the conversion rates stored in a database table or XML file.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
sAuhsoj

sAuhsoj

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
Thanks.
I would set this up but I am limited to client-side languages, don't know ajax and am content with the javascript - I think I have most of the way

#6
sAuhsoj

sAuhsoj

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
what is wrong with the javascript? I should be able to work this out but I can't see the flaw

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The biggest flaw, to me, is that conversion rates change from day-to-day, or even hour-to-hour. Having to manually rework the JavaScript for dozens of monetary units is error-prone and tedious.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
sAuhsoj

sAuhsoj

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
Thanks a lot for putting so much thought into this. However, I do not plan on using calculating the exact right cost based on the exchange rates as I do not think that this is good business. I am not really planning on updating the exchange rates but I may change the costs from time to time. I may not have been clear on this before.
Do you know how I can make the javascript work?

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It should be working fine. As usual, however, you should validate the form data on the server side. If you need to test it, I recomment using the FireBug plugin for FireFox and setting a breakpoint in the script to test it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users