Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Please help me control forms

    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:
    Code:
    <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!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    You should move the execution of the function from the image onclick to the form onsubmit
    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.
    Why does clicking the image submit the form in the first place?
    Is this all I need to make it work?

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    An input type="image" essentially is a submit button. Clicking it will submit the form by default.

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, thanks a lot!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •