ASP.net Paypal button HTML Form Post Issues.

by neo_savvy 2/9/2008 11:07:00 AM

Have you followed all the instructions on Paypal when trying to setup a Paypal button in ASP.NET but get nothing but a page refresh (without a redirect) when you click the button? The answer is very simple. HTML forms cannot be nested. You need to add some javascript that will modify the form's "action" property. This will allow the page to the submitted to Paypal.

Here's the sample of the HTML generated on the Paypal site for the encrypted HTML code button:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHVwYJKoZIhCCB0QCAQExggEwMIIBLAIBADCBlDCCSqGSIb3DQE=-----END PKCS7-----">
</form>

Here's a modified version of the above HTML code that solves the nested HTML form problem:

<input type="hidden" name="cmd" value="_s-xclick" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!" onclick="document.getElementById('aspnetForm').action='https://www.paypal.com/cgi-bin/webscr';" />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHVwYJKoZIhCCB0QCAQExggEwMIIBLAIBADCBlDCCSqGSIb3DQE=-----END PKCS7-----"/>

By eliminating the "form" tag and adding an onClick attribute to the input tag for the Paypal button image, we can use javascript to modify the action property of the HTML form.

document.getElementById('aspnetForm').action='https://www.paypal.com/cgi-bin/webscr';

Make sure that the form id matches what you have. I hope this helps.

Currently rated 5.0 by 7 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Related posts

Comments

7/20/2008 11:48:03 PM

Hi Chyke!

Thanks for this fine piece of code that saved me hours of work!

I had a problem, though, while adding more thatn one button to my page. It took me a while to figure out, but here's what I've done:

- I've left the <form> tag around each PayPal button (sorry for W3C compliancy...).
- I've used the same onclick event you did, with the name of my asp.net form.

I don't understand exactly why it works, but it does (at least under IE7). Somehow, the form making the post is the asp.net form, but only the hidden fields inside the pressed button form are sent.

I still need to test under IE6, FireFox and Safari to be sure it really works.

Julien Bérubé ca

9/21/2008 11:40:36 AM

Quick question from a newbie. When you refer to the Form Id which I assume it the 'aspnetform'. Is this the FormId in the Masterpage. Thanks in advance.

Ira

Ira us

1/25/2010 4:38:51 AM

thanks! very helpful post!! like the template btw Wink

Nice Post us

1/27/2010 11:46:28 AM

I did try to add this code

<input type="hidden" name="cmd" value="_s-xclick" />
<input type="image" src="https://www.paypal.com/.../btn_donateCC_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!" onclick="document.getElementById('aspnetform').action='https://www.paypal.com/cgi-bin/webscr" rel="nofollow">www.paypal.com/cgi-bin/webscr';" />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHVwYJKoZIhCCB0QCAQExggEwMIIBLAIBADCBlDCCSqGSIb3DQE=-----END PKCS7-----"/>



on my aspx page but i didn't have to add this following line to any page.

document.getElementById('aspnetform').action='https://www.paypal.com/cgi-bin/webscr" rel="nofollow">www.paypal.com/cgi-bin/webscr';

It works on IE8 but it didn't work on Firefox (it just refresh my page on firefox). Where do I have to add that single line to? Thanks in advance!

nan us

Add comment


 

  Country flag





Live preview

9/3/2010 9:45:54 PM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Chyke Ucheya Chyke Ucheya
I'm a Software Engineer at a leading Technology Consulting firm in Atlanta, GA.

E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in