PayPal Hacks. 100 Industrial-Strength Tips and Tools Free Open Book

PayPal Hacks. 100 Industrial-Strength Tips and Tools

Previous Section  < Day Day Up >  Next Section

Hack 32 Provide Purchase Options with Drop-Down Listboxes

figs/beginner.gif figs/hack32.gif

Change a few lines of the PayPal Button Factory code to restrict purchase options to a distinct list of choices.

By default, the item_name variable created by the PayPal Button Factory [Hack #28] is a hidden field containing a single string of text, which means that a single payment button corresponds to a single product. So, if you sell three products, you'll need three payment buttons, right?

Not so, thanks to drop-down listboxes.

Since many of the products you're selling probably come in a combination of styles or sizes, you can merge those variations into a single purchase button. For instance, if you're selling clothing, a Size option might contain three choices: Small, Medium, and Large. Fortunately, PayPal doesn't distinguish between text strings sent from text boxes and list elements selected from drop-down listboxes, so you can easily replace any <input> field with a <select> drop-down list. For instance, take:

<input type="hidden" name="item_name" value="T-Shirt">

and replace it with:

<select name="item_name" id="item_name">

  <option>T-Shirt</option>

</select>

The problem here is that we still provide the customer with only one option. To add more options, simply insert additional <option> tags, one for each variation, like this:

<select name="item_name" id="item_name">

  <option>T-Shirt, Small</option>

  <option>T-Shirt, Medium</option>

  <option>T-Shirt, Large</option>

</select>

Figure 4-4 shows the completed drop-down listbox.

Figure 4-4. Taking advantage of PayPal's option fields with a simple drop-down listbox.
figs/pph_0404.gif


With this simple change, your customers choose a size, click the Buy Now button, and pay for your item. PayPal then sends the customer's selection back to you in the "You've got cash" email.

If you need to provide your customers with more than one option, you can include up to two additional option fields [Hack #33] and convert both of them to drop-down lists with this same procedure. Thus, you can have up to three different options with a single payment button.

4.6.1 Hacking the Hack

You can take this hack a step further by changing the values of other fields based on selection. For instance, you can change the price based on the shirt size your customer chooses and send the correct price to PayPal along with the corresponding options. You need to add a few pieces of code to your payment button form for this to work.

First, place this JavaScript code in the section of your page between the <head> and </head> tags:

<script type="text/javascript">

<!-- Update Price Change

function UpdateForm (object1) {             // process change selects

var i,item_amt,object,position,val;

  item_amt = object1.amount.value;          // default amount

  for (i=0; i<object1.length; i++) {        // check options

    object = object1.elements[i];           

    if (object.type == "select-one" &&

        object.name == "cng") {             // must be named cng

      position = object.selectedIndex;      // option selected

      val = object.options[position].value; // selected value

      position  = val.indexOf ("$");        // set new price

      if (position >= 0) item_amt = val.substring (position + 1)*1.0; 

    }

  }

  object1.amount.value = item_amt;

  if (object1.item_total) object1.item_total.value = "$" + item_amt;

}

//-->

</script>

Next, change the <form> tag for your payment button code so the JavaScript function is executed when the form is submitted, like this:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" 

onsubmit="this.target='paypal';UpdateForm(this);">

Finally, modify the <select> tag so that it, too, is linked to the JavaScript code:

<select name="cng" onchange="UpdateForm(this.form);">

 <option value="Small $1.00">Size: Small $1.00</option>

 <option value="Medium $2.00">Size: Medium $2.00</option>

 <option value="Large $3.00">Size: Large $3.00</option>

</select>

You can edit the amount charged to your customer by changing the value="Small $1.00" section of the form field. You can also change the text displayed to your customer by changing the value between the <option> and </option> sections.

Make sure the amount tag in your form is set to the same value as the default value of the drop-down menu. That way, if the form is submitted without changing the values, the amount has the correct default value.

When this code is in place, the price is updated automatically whenever a new size is selected.

Since this solution relies on JavaScript to update the price according to a customer action, it will fail if the customer has disabled JavaScript. Although PayPal doesn't do price checking, you can effectively prevent this problem by checking for JavaScript before displaying order pages to your customers.


    Previous Section  < Day Day Up >  Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


         Main Menu
    PayPal Hacks
    Table of Contents
    Copyright
    Credits
    Preface
    Chapter 1. Account Management
    Chapter 2. Making Payments
    Chapter 3. Selling with PayPal
    Chapter 4. Payment Buttons
    Introduction: Hacks #28-44
    Hack 28 Create a Buy Now Button
    Hack 29 Use a Custom Button Image
    Hack 30 Create a Purchase Button for Services
    Hack 31 Create an Auction Payment Button
    Hack 32 Provide Purchase Options with Drop-Down Listboxes
    Hack 33 Include More Than Two Option Fields
    Hack 34 Override Shipping and Handling Preferences
    Hack 35 Build Notification Tracking
    Hack 36 Hack-Proof Your Payment
    Hack 37 Hack-Proof Your Buttons with Encryption
    Hack 38 Include Payment Buttons in Email Messages
    Hack 39 Hide Your Email Address from Spammers
    Hack 40 Accept Donations
    Hack 41 PayPal-Enable Your Flash
    Hack 42 Get More Out of Dreamweaver and PayPal
    Hack 43 Provide Options with ASP.NET Web Controls
    Hack 44 Try Accepting Payments in a Bogus Currency
    Chapter 5. Storefronts and Shopping Carts
    Chapter 6. Managing Subscriptions
    Chapter 7. IPN and PDT
    Chapter 8. The PayPal Web Services API
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele