How to subscribe to click event of html form?

回眸只為那壹抹淺笑 提交于 2019-12-20 06:49:45

问题


I have a google checkout "buy now" button and I want to add dynamically created information to send when it's clicked. How do I do that?

The button's html is:

<form action="https://sandbox.google.com/checkout/..." id="Form1" method="post" name="..." target="_top">
<input name="item_name_1" type="hidden" value="..." />
...
<input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=..." type="image" />
</form>

So what do I add? (And if possible, I'd like to do that through ASP.Net code.)

I tried adding onclick="Button1_Click" but it didn't work.


回答1:


If this is for Google Checkout, you should consider using the existing .Net library for Google Checkout.

While the above suggestion will work, the correct (secure) way of doing so is documented here. The XML alternative (signing) is here, one of which allows you to do a direct FORM POST to Google in a secure manner.

Submission via client methods will always make you susceptible to tampering - it would make little difference (security-wise) than just doing a basic HTML form post to Google.


Updated:

...doesn't support buy now...

At the end of the day, there are 2 things that make "BUY NOW" different from any other "cart" submission:

  1. the image
  2. single item purchase for Buy Now (vs. one or more for other implementations).

See this sample code from the .Net Library. It should give you all you need....


Update 2:

There isn't anything "rigid" about the library. You don't have to use everything in it - you can even just take advantage of all the plumbing already created for you.

Simple Example, implementing BUY NOW, using Google Checkout .Net Library

Web Forms Code (aspx):

<p>Some ASP.net button:<br />
<asp:Button ID="Button1" runat="server" Text="BUY ME NOW" onclick="Button1_Click" /><br />

An ASP.NET Image Button using BUY NOW Image:<br />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="https://checkout.google.com/buttons/buy.gif?merchant_id=[USE YOUR OWN MERCHANT ID]&amp;w=117&amp;h=48&amp;style=white&amp;variant=text&amp;loc=en_US" onclick="ImageButton1_Click" />

At this point this is what you see in your browser:

Code (inline or code-behind):

using GCheckout.Checkout;
using GCheckout.Util;

....

protected void Button1_Click(object sender, EventArgs e)
{
    doSomething();
}

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    doSomething();
}

private void doSomething()
{
    /**
     * Use the correct Merchant ID and Key based on GCheckout.EnvironmentType
     * You cannot mix/match sandbox and production id or key
     */
    CheckoutShoppingCartRequest Req = new CheckoutShoppingCartRequest("your_production_MID", "your_production_KEY", GCheckout.EnvironmentType.Production, "USD", 20);

    /**
     * Everything from this point is a copy and paste
     * from .net libary sample code
     * http://code.google.com/p/google-checkout-dotnet-sample-code/source/browse/examples/post_cart/simple.aspx
     */
    Req.AddItem("Mars bar", "Packed with peanuts", 0.75m, 2);

    //lets make sure we can add 2 different flat rate shipping amounts
    Req.AddFlatRateShippingMethod("UPS Ground", 5);

    //Add a rule to tax all items at 7.5% for Ohio
    Req.AddStateTaxRule("OH", 7.5, true);

    GCheckoutResponse Resp = Req.Send();
    if (Resp.IsGood)
    {
        Response.Redirect(Resp.RedirectUrl, true);
    }
    else
    {
        Response.Write("Resp.ResponseXml = " + Resp.ResponseXml + "<br>");
        Response.Write("Resp.RedirectUrl = " + Resp.RedirectUrl + "<br>");
        Response.Write("Resp.IsGood = " + Resp.IsGood + "<br>");
        Response.Write("Resp.ErrorMessage = " + Resp.ErrorMessage + "<br>");
    }
}

Important

The above is just sample code to illustrate what you can do with the .Net Library. To comply with Google Checkout implementation policy, use the ImageButton implementation - it ensures that you are using Google's BUY NOW button (unaltered, etc.).




回答2:


ASP is a server-side script, so you can't launch it after the page is loaded. The onlcick attribute is used to launch javascript. Two workaround's:

  • What you can do is redirect it to another page, which redirects to google checkout.
  • Using javascript and ajax, load the dynamic information, and submit it using javascript


来源:https://stackoverflow.com/questions/11118890/how-to-subscribe-to-click-event-of-html-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!