GET fetches data from a server, while POST sends data to it
GET and POST requests are not unique to PHP, but it’s still something that you need to understand. HTTP requests allow for communication between the client and the server. In the previous article, we saw that communication when we submitted a form. The form data was entered on the client side (the user’s computer) and when the Submit button was pressed, the data was sent to the server.
This might not have been completely clear since our server was running on our computer, but imagine that you deployed your code to an AWS EC2 instance. That server is not on our machine anymore so the process is somewhat clearer. We’ll walk through an example shortly, but let’s look at the various communication methods.
https://blog.devgenius.io/php-p74-forms-introduction-6f8838e0d16
Common Request Methods
There are a few request methods but I wanted to introduce a few that will make it to your daily life as a developer:
GET
: used to request data from the server. Data can be sent to the server through the address bar as key/value pairs. You’ve probably seen an address like this: https://www.dinocajic.com/?p=123. In this case, p
is the key and 123
is the value. It just returns an image.
POST
: used to send data to the server. Without going too much into the details, data will be sent to the server and will be available to the server through the $_POST
array. Most of the time, the data that’s sent to the server via post
is done through a form. The form field will have a name
attribute, which will be the key and the data that’s entered by the user will be the value. For example, <input type="text" name="full_name">
will have a key of full_name
and the data that the user enters, i.e. Dino Cajic
, will be the value. If you echo $_POST['full_name']
on the server side, you’ll receive Dino Cajic
. Unlike the get
method, data submitted via the post
request is not visible to the user in the URL.
PUT/PATCH
: Sends data to the server notifying it that the resource is going to be updated. There are a couple of differences on when to use one over the other, but they both update the resource. Most of the time, it’s just based on developer preference as to which one is used. Frameworks like Laravel allow for resource management by easily funneling requests like this one through. Can you use the post
request to update something in the database? Of course. You’re still sending data to the server. This is just more explicit.
DELETE
: Similar to the put/patch
request, this is another explicit request that allows for data to be sent to the server. In this case, we’re specifying that we want to delete a resource.
Walking Through A Form Example
In the previous article, we created a form that allows the user to submit their full name.
<form action="form_processor.php" method="post">
<div>
Full Name: <input type="text" name="full_name">
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
<?php
echo $_POST['full_name'];
Let’s see how the data moves.
- The user enters a url in their browser. Since we were hosting the server on our machine, the url was
https://0.0.0.0/form_example.php
orhttps://127.0.0.1/form_example.php
. - When the user presses Enter, a
get
request is sent to the server to return the contents ofform_example.php
. The server has access to the$_GET
variable, but no data was passed via the url string. We also didn’t code for anything to be done with the contents from the$_GET
variable. - The server gets the request, goes into
form_example.php
, sees that html data is to be returned, and returns the html data. - The browser receives the html data and transforms it to something that the user can see. In this case, the user sees a form that allows them to enter their full name and press the Submit button.
- The user enters their full name and presses the Submit button.
- The form
action
specifies that the data should be sent tohttps://127.0.0.1/form_processor.php
and the way that it should be sent to the server was defined with the form method set equal topost
. - A
post
request is sent to the server. The server has access to the$_POST
variable. We know that the$_POST
variable will have afull_name
key. - The server processes our code, which is to
echo $_POST['full_name']
. Once the data is generated, it’s sent back to the client. - The user sees what they just entered.
Changing the Form’s Method from POST to GET
What if we changed the form method from post
to get
. Can form data be sent to the server? Let’s see.
<form action="form_processor.php" method="get">
<div>
Full Name: <input type="text" name="full_name">
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
Since data is sent via the get
request, we won’t have any data in our $_POST
array. It should be in our $_GET
array. In the form_processor.php
file, let’s var_dump
our $_GET
variable.
<?php
var_dump($_GET);
If we submit the form, we’ll see that it works. We get the following data.
/app/74 Form Requests/form_processor.php:3:
array (size=1)
'full_name' => string 'Dino Cajic' (length=10)
The interesting part is that the data was moved to the URL when we hit submit. Take a look at your URL. You should see something similar to this:
https://0.0.0.0/form_processor.php?full_name=Dino+Cajic
The variables were appended to the url string. This is sometimes desired, for example when you want to be able to bookmark form results, but it could also be undesired, for example when you submit your username and password.
One thing to note is that when you submit a form with the post
request, the back button in your browser will ask if you want to resubmit the form. With the get
request, you just get the normal back-button functionality.
Passing Data Without a Form
I’m sure you’re already connecting the dots. Do you even need a form? Can you not just send variables through the URL string. Yes you can! Let’s create a new file. We’re going to state that we accept the first_name
and last_name
keys to exist in the URL string.
<?php
echo $_GET['first_name'] . " " . $_GET['last_name'];
Now, type in the URL with those keys and pass a value that you want.
https://0.0.0.0/get_request_example.php?first_name=Dino&last_name=Cajic
You start your first variable after the ?
symbol. ?first_name=Dino
. Your second variable and onwards will be appended after the &
symbol: &last_name=Cajic
.
Once we hit Enter, the get request is sent along with the data we just entered in the url string and the server processes it by echoing out the data in the first_name
and the last_name
.
What if we don’t pass those values, you get warning messages:
Warning: Undefined array key "first_name" in /home/user/scripts/code.php on line 2
Warning: Undefined array key "last_name" in /home/user/scripts/code.php on line 2
You could always check to see if the data is present before echoing out the data. You’ll do this with the isset()
function that checks to see if an array key exists and is not null
.
In this instance, the data is displayed only when both the first_name
and last_name
parameters are defined and data is passed, otherwise the user will see the You must have both the first_name and last_name defined
message.
FORMS CAPTURE DATA, CONNECTING USERS AND SYSTEMS.
We’ve gone through enough syntax that we’re ready to start looking at some actual concrete examples, like form processing. We’re going to introduce databases and data-persistence in a later article. All we want to know is how do we send data from a form on the client end to a server, wherever that server may live.
GET fetches data from a server, while POST sends data to it
PHP – P75: GET and post request methods
GET and POST requests are not unique to PHP, but it’s still something that you need to understand. HTTP requests allow for communication between the client and the server.
ACCESS SERVER INSIGHTS WITH PHP’S BUILT-IN PREDEFINED VARIABLES
PHP – P76: PREDEFINED VARIABLES
PHP automatically has access to form submission variables since they’re added to the $_POST variable. Let’s dive into the most common variables like $_SERVER, $_GET, and $_POST.