Making 'Method Get' Work As 'Method Post'
Yesterday, I needed to have a clickable link (which is method GET) send information to a script that expects to receive it as method POST.
The script generally accepts information from a form. It's a form handling script. The form is submitted method="post" and the form handling script doesn't recognize any other submission method.
This is what I had:
-
A form that submits method="post".
-
A PHP script that expects data from a form submitted method="post".
-
A regular web page link with information appended to the URL (like ?name=value&name2=value2).
As a-tag links do, a clicked link causes a method "get" request. Somehow, that information needed to arrive at the script as a method "post" submission.
In essence, this is the way it's done:
-
The URL of the web page link is to a special relay script.
-
The relay script accepts the method get information from the link, composes a method post response, and submits the data to the form handling script.
-
The form handling script accepts the data as if it was submitted with a method="post" form.
(The way it was done would work for software written in any server-side language that accepts method="post" form submissions and has no automatic-submission protection. You're not restricted to PHP.)
About the Relay Script
The relay script is Get To Post Relay. It accepts the data from the link, then composes a method="post" form and automatically submits it to the form handling script.
Generally, the relay happens so fast the person who clicked the web page link doesn't notice any delay.
But servers do sometimes respond slowly. If the relay takes more than 3 seconds (a very long time), the person is presented with a form button to click for continuing.
Do You Need It?
Maybe you don't need the functionality. Now. But hold onto the script and instructions anyway. Because if you do need it you're likely to need it very much.
Forms with only a submit button can be pre-filled with type="hidden" fields so people can click the link to proceed. But sometimes a form isn't feasible and a regular link must be used. Links in email, for example — perhaps a link to send the subscriber's address and their "yes" or "no" answer to a script.
The Relay Script Source Code
Below is the relay script. Instructions follow.
No customization is necessary. Simply copy it, name it get2post.php, and upload it to your server. Make a note of its URL. (It can be given a different name ending with ".php", but the instructions assume get2post.php)
<?php /* Get To Post Relay (Submit received GET values as a POST form submission.) Version 1.0 November 22, 2015 Bontrager Bontrager Software LLC https://www.willmaster.com/ */ $FormDestination = htmlspecialchars($_GET['dest']); unset($_GET['dest']); echo <<<PAGETOP <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Get values to post values relay</title> </head> <body> <form id="auto-submit-form" method="post" action="$FormDestination"> PAGETOP; foreach( $_GET as $k => $v ) { $key = htmlspecialchars($k); $val = htmlspecialchars($v); echo "<input type='hidden' name='$key' value='$val'>"; } echo <<<PAGEBOTTOM <div id="submit-button" style="display:none; text-align:center; margin-top:1in;"> If page doesn't automatically complete, then click the submit button.<br> <input type="submit" value="Submit"> </div> </form> <script> setTimeout("document.getElementById('submit-button').style.display='block'",3000); document.getElementById('auto-submit-form').submit(); </script> </body> </html> PAGEBOTTOM; exit; ?>
The URL of the above relay script when uploaded to your server will be the URL of the web page link. For the examples, I'll assume the relay script is at http://example.com/get2post.php
Using the Relay Script
How to construct the Get to Post web page link:
The web page link URL is
http://example.com/get2post.php?dest=______
and the underline replaced with the URL of the form handling script and all the information the form handling script expects.
As an example, if the person's name and email address is embedded in a link to sign up to a newsletter, it might be constructed something like this:
<a href="http://example.com/get2post.php?dest=http://domain.com/subscription.php&name=Joe&email=joe@example.com"> Click here to subscribe! </a>
Following the link URL is the "?" character (colored gold), which denotes the beginning of information attached to the URL.
The name "dest" and its value (colored red) specify the URL of the form handling script that would normally receive the method="post" submission. The rest of the name and value pairs (colored blue) are information that would normally be provided when using a form.
Each name and value pair is separated with a "&" character (colored green)
When the link is clicked, the form handling script will receive the information as method="post".
The Data for the Link
Before a Get to Post web page link can be constructed, the information to construct it with must, of course, be known. Just like pre-filling form fields, the data to do it must be known.
When form fields are pre-populated on web pages, it's generally accomplished with either JavaScript or PHP from a cookie or with PHP with data from a database lookup.
The data in the Get to Post web page link would be constructed similarly, except for constructing a &name=value URL instead of a form field with a name and a value.
For outgoing emails sent by an emailing service, the email address of the recipient and the recipient's name are generally available for putting into the email content. Use those placeholders to construct the Get to Post link within your newsletters.
Everything that the form handling script needs must be part of the link. Then you're good to go.
(This article first appeared in Possibilities ezine.)
Will Bontrager