JavaScript, Not PHP, for PayPal Thank-You Page
When a web page is requested, the requested URL may have information attached to it. The information follows a ?
character in the URL and is generally referred to as a URL parameter.
In this example URL, the blue colored part of the URL is the parameter information.
http://example.com/page.html?amt=123.45&tx=34EDFKEI9
The parameter information generally contains name and value pairs separated with an &
character. In the example, the name and value pairs are amt=123.45 and tx=34EDFKEI9.
I'll show you how to use JavaScript to insert the values 123.45 and 34EDFKEI9 into the web page, using the names amt and tx as keys.
If you buy things via PayPal, you may recognize the two key=value pairs in the example URL. Unless the merchant instructs otherwise, PayPal returns you to the merchant's thank-you page with the amount and transaction ID information in amt and tx keys.
And there's a reason for that.
You'll find the source code for a PayPal thank-you page at the PayPal Send‑Money Form article. The thank-you page uses PHP to insert the parameter information into the content of the page.
I'll show you how to update that thank-you page so it inserts the parameter information with JavaScript instead of PHP.
This is the part of the thank-you page that will be converted from PHP to JavaScript. (The source code for the complete, converted thank-you page is near the end of this article.)
<p> Transaction amount: <?php echo(@$_GET['amt']); ?> <br>Transaction ID: <?php echo(@$_GET['tx']); ?> </p>
The first step is to put this JavaScript into the head
area of the thank-you page or, alternatively, anywhere on the page above where the parameter information will be published. No customization is required.
<script type="text/javascript"> var URLparameterInfo = new Object; if(location.search && location.search.length>1) { var ta = location.search.substring(1).split("&"); for(var i=0; i<ta.length; i++) { var tta = ta[i].split("="); URLparameterInfo[tta[0]] = tta[1]; } } </script>
Now, let's change the two places in the thank-you page where PHP is used so it uses JavaScript, instead.
-
Replace this:
<?php echo(@$_GET['amt']); ?>
With this:
<script type="text/javascript"> if(URLparameterInfo.hasOwnProperty("amt")) { document.write(URLparameterInfo["amt"]); } </script>
-
Replace this:
<?php echo(@$_GET['tx']); ?>
With this:
<script type="text/javascript"> if(URLparameterInfo.hasOwnProperty("tx")) { document.write(URLparameterInfo["tx"]); } </script>
When you have replaced both PHP sections with JavaScript, this is what that part of the thank-you page will look like:
<p> Transaction amount: <script type="text/javascript"> if(URLparameterInfo.hasOwnProperty("amt")) { document.write(URLparameterInfo["amt"]); } </script> <br>Transaction ID: <script type="text/javascript"> if(URLparameterInfo.hasOwnProperty("tx")) { document.write(URLparameterInfo["tx"]); } </script> </p>
Here is the entire thank-you page converted from using PHP to using JavaScript.
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Thank You</title> <script type="text/javascript"> var URLparameterInfo = new Object; if(location.search && location.search.length>1) { var ta = location.search.substring(1).split("&"); for(var i=0; i<ta.length; i++) { var tta = ta[i].split("="); URLparameterInfo[tta[0]] = tta[1]; } } </script> </head> <body> <h1>Thank You</h1> <p> Your transaction has been completed and PayPal will email a receipt to you. </p> <p> You may log into your account at <a href="https://www.paypal.com"><nobr>https://www.paypal.com</nobr></a> to view details of this transaction. </p> <p> Transaction amount: <script type="text/javascript"> if(URLparameterInfo.hasOwnProperty("amt")) { document.write(URLparameterInfo["amt"]); } </script> <br>Transaction ID: <script type="text/javascript"> if(URLparameterInfo.hasOwnProperty("tx")) { document.write(URLparameterInfo["tx"]); } </script> </p> </body> </html>
As you can see, when it comes to inserting URL parameter information into a web page, JavaScript can be used instead of PHP.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager