Probably you have already heard backend guys talking about sending some mysterious POST requests or something similar. What does it mean though?
POST request
POST method is used to send data to a server to create/update a resource, or manage the sent data. The data can then be used in the server however you want.
So for example, if we have a contact form on our website, user fills all the fields and clicks “Submit”… now what? Well, now we need to write a PHP program which would handle all the data that was inserted into the form and send an email to the site owner. So the algorithm would be:
User has clicked the Submit button
Use POST method to send the inserted data to the server
Store the data of each field in different variable
Use mail() function to send an email out and use these variables in the letter
Show a “thank you for your letter” message
You and your server have full control over this posted data. You can echo it on the page, or insert it into your database, or run if-else statements.
Let’s try the POST request
Let’s start with the simplest POST request. For that, we need a form written in HTML, and then with PHP, we can check if the form was submitted, and react accordingly.
1. Let’s create a form in HTML. In the <form> tag, we leave the action empty. That way, it will reload the same page after form submission. Method is POST, meaning after submission, all the fields are posted to the server.
Above code will render a simple HTML form, with an input field and a submit button. After clicking the Send button, all the values of the fields inside this form are POSTED (sent) to the server.
2. Insert this PHP code somewhere into your page. Make sure to wrap your PHP code into <?php and ?> tags.
If something, with name attribute “send” was posted, echo the value of the input with name attribute “email”.
3. Go to your site, and submit your form. Now, after form submission, you should see instantly, what you just submitted.
GET request
GET requests are the opposite. We use them to retrieve data from the site instead. WIth GET request, we can get information from the URL. Sometimes you can notice that the URLs of some sites look like this:
With a GET request, we can retrieve data from inside the URL. Read that URL carefully, and you can notice 3 different variables in it with defined values: โsearchโ โ โhelloโ โpageIdโ โ 19 โusernameโ โ โadminโ
They are separated from each other with ampersands (&). The first variable is always separated by question mark (?). So the structure is always like this: ?var1=this & var2=that & var3=somethingelse
Let’s take Google search as a simple example. When you do a search on Google, you’ll see lots of search variables in the URL. We can run Google search with one paramenter as well. Try this: visit the URL https://www.google.com/search?q=Hello You can then see, that the same keyword as in variable $q, is entered into the search box. Google makes a GET request — fetches your search query from the URL and then shows the according results with $_GET[‘q’] request. So inside their search box, there could be a code like: <?php echo $_GET[‘q’]; ?>
When we have an URL with different variables, we can echo them out or use them in some other ways in our PHP file with $_GET[‘variable’] command.
Let's say the index page of your website has the above PHP code. And then you visit your index page, with these parameters below. https://mywebsite.com/?search=hello&pageId=19&username=admin
What would you see on your page?
In PHP, we use GET requests to retrieve from URL, which page the user is currently viewing, and by that, show the page content.
Independent exercise #1
POST request
Create a calculator which would sum two numbers.
Create an HTML form with POST method
Have two fields inside the form. Use input type=”number” and give them names (e.g. num1 and num2)
Create a submit button for your form
Write PHP code, that would check if the form was submitted
If the form is submitted, put define the posted values as variables, e.g $number1 = $_POST[‘num1’];
Echo the sum of these two variables.
The result should look like this:
Independent exercise #2
GET request
Write a PHP script which would echo some random custom variable from the URL. So if you visit your site with some parameter, it would print it out on the website.
For example, by visiting such URL with $somecolor variable, it would just echo “blue” on your website. https://yoursite.com/?somecolor=blue