Hack 79 Display Donation Goals on Your Web Site

figs/moderate.gif figs/hack79.gif

Use donation buttons and IPN to display actively updated donation goals.

As a web site owner, you might want to provide information or entertainment to your visitors without charging for the service or cluttering up your site with advertising. However, you might also need funds to pay site expenses or to support a worthy cause. The PayPal Donate Now button enables webmasters to collect payments from willing donors.

Donation buttons on web sites do not give visitors much information apart from the cause to which they are donating. Contributors have no idea how many other people have donated or how much has been raised already. Visitors might be more inclined to donate once they know others have, or if they believe their donation will make a difference in achieving a goal for a fund drive. Providing donation goals and a tally of the amount collected to date can induce potential donors to contribute?and contribute in larger amounts.

Another way to entice donors is to offer several suggested donation levels [Hack #40] .

This hack illustrates how to use your donation button to display a donation goal and the current amount collected. To implement this hack, you need to set up your site to receive Instant Payment Notifications [Hack #65] and connect the notifications to a local database using dynamic server page technology. This example uses VBScript for ASP, but it could as easily be done with PHP, Perl, Python, or Java.

7.19.1 Recording Donations

To keep a record of donations as they are made, first install a script to process PayPal's IPN feature and add a record to your database for each transaction [Hack #82] .

Next, use a SQL query such as this one to get the sum of all donations in your database:

SELECT SUM(mc_gross) AS TotalDonated FROM tblOrders

See the "Database Coding and Platform Choices" section of the Preface for the additional information needed to put this SQL statement to work with this and the other hacks in this book.

For instance, if you have a table that looks like Table 7-3, the SUM(mc_gross) function returns the sum of the mc_gross column ($323.10 in this case).

Table 7-3. A database table to track the donations













Put the result into the rsDonationGoal("TotalDonated") variable. If you've received three donations for $3, $5, and $7, respectively, the value for rsDonationGoal("TotalDonated") will be $15.

Naturally, if you're accepting donations for more than one cause, you'll need to narrow the SQL query so that it returns only donations that relate to the donation goal at hand.

7.19.2 Building the Donation Page

The donation page consists of three items: your donation goal (in dollars) as static text, the total amount collected thus far (drawn from your database), and the PayPal donation button (displayed somewhere prominently, of course):

<p>Please help us achieve our donation goal of $10,000.</p>

Total Amount collected so far: <%=rsDonationGoal("TotalDonated")%>


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

<input type="hidden" name="cmd" value="_xclick">

<input type="hidden" name="business" value="sales@paypalhacks.com">

<input type="hidden" name="item_name" value="Donation">

<input type="hidden" name="item_number" value="Donation-001">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="hidden" name="tax" value="0">

<input type="image" src=


                border="0" name="submit">


7.19.3 Hacking the Hack

You might also want to display the number of donations you have already received. Start by adding another SQL query to calculate the count of donations table:

SELECT COUNT(Id) AS CountDonated FROM tblOrders

Then, use this new CountDonated variable in your ASP page:

Total number of donations collected so far: <%=rsDonationGoal("CountDonated")%>

Or, calculate the average donation with this bit of SQL:

SELECT AVG(mc_gross) AS AverageDonated FROM tblOrders

and display it on your ASP page:

Average Donation Amount: <%=rsDonationGoal("AverageDonated")%>

All this extra information makes your cause appear more credible and helps donors pony up the dough. If you really want to make it fancy, you can display a recent donor list [Hack #80] on the same page.