0

I have the following code that allows the person to choose the donation amount and download the file. At the moment this allows the person to put in 0 and download. I want to make the minimum 0.99. How could I make it compulsory to make a donation otherwise don't download. I appreciate your help.

 <?php

echo "<p>Donate fixed amount to CharityName</p>
<form method='POST' action=''>
<select name='currency_code'>
<option value='EUR'>EUR</option>
<option value='GBP'>GBP</option>
<option value='USD'>USD</option>
<input type='text' name='donate_amount' value='0'>
<input type='submit' name='submit' value='Donate'></form>";

if(!empty($_POST['submit'])) {
// Form has been submitted
  if($_POST['donate_amount'] > 0) {
// Redirect to PayPal
header('Location: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&item_name=Donation for XXX&amount='.$_POST['donate_amount'].'&currency_code='.$_POST['currency_code'].'&[email protected]&cbt=Download the file&return=http://link-to-the-file&cancel_return=http://back-to-my-website');
}
else {
// No donation amount entered - proceed to file directly
header('Location: http://link-to-the-file/');
}
}

?>
3
  • I was also wondering how I could include a google analytics code in this download link to track the download that was made
    – Hue
    Aug 17, 2012 at 21:27
  • remove the header('Location: http://link-to-the-file/'); in your else and add error message there.
    – Virendra
    Aug 17, 2012 at 22:44
  • "Compulsory to make a donation"... I don't think you understand what a donation is. Aug 19, 2012 at 1:14

1 Answer 1

0

See a sample:

if(!empty($_POST['submit'])) {
    if($_POST['donate_amount'] >= 0.99) {
        // Redirect to PayPal
    } else {
        exit('Please donate at least $.99');
    }
}

For Google analytics, I can't answer you, sorry.

3
  • Okay, I think I have put that together - thank you for your help. Is there a way of adding google analytics code to track the download? Adding in: <a href=”pdfs/my-file.pdf” target=”_blank” onclick=”_gaq.push(['_trackEvent','Download','PDF',this.href]);”>Download my file</a>
    – Hue
    Aug 18, 2012 at 9:26
  • also, would I have to put 0.98, so that 0.99 is the minimum? As with 0.99 it seems to not redirect to Paypal.
    – Hue
    Aug 18, 2012 at 9:30
  • For the Google analytics, would I be best to do this? : $(function(){ $('#submit').click(function(){ _gaq.push(['_trackEvent', '<?php the_title(); ?>', 'my action']); }); });
    – Hue
    Aug 18, 2012 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.