$_POST and $_GET arrays in PHP form processing
Forms are part of the HTML language. Forms are needed to transfer data from the client to the server. Most often, forms are used to register users, fill out questionnaires, place an order in an online store, and so on.
Through forms, you can send both simple text information and files.
Most of the time you're programming in PHP, you'll be working with forms and form data in one way or another.
HTML describes what elements the form consists of and how it looks. But without a host, that is, a server that receives this data and processes it in the right way, there is no point in creating forms.
PHP contains many tools for working with forms. This makes it very easy to solve typical tasks that often arise in web programming:
- User registration and authentication;
- Sending comments on forums and social networks;
- Making orders.
Almost any modern site contains at least a few different HTML forms.
Submitting a form
Consider one typical example - a feedback form. To connect users with the authors of the site, as a rule, feedback forms are used, where a person indicates the name, email for feedback and the text of his message.
Such a form in HTML might look like this:
<form name="feedback" method="POST" action="form.php">
<label>Name: <input type="text" name="name"></label>
<label>Email: <input type="text" name="email"></label>
<label>Message: <textarea name="message"></textarea></label>
<input type="submit" name="send" value="Submit">
</form>
This is a very simple form, consisting of three fields and one submit button.
Almost all of the above code describes the appearance and content of the form, but you should pay attention to the two attributes of the tag <form>
, which are needed to indicate how the data is processed:
method
- This attribute is used to define the HTTP method that will be used to send data to the server. You are already familiar with the HTTP GET method, which tells the server to simply return a specific document.
The POST method indicates the intention to send some information to the server, which, however, does not cancel the subsequent receipt of content.
action
- contains the address of the PHP script that should process this form.
After clicking on the "send" button, the browser performs a POST request with the entered data to the address specified in the action attribute.
Form processing
After submitting the form, control is transferred to the PHP script, which must receive the submitted data, perform some action with it (for example, save it to the database) and show the result.
The result may be some message about the successful completion of the operation, for example, "your data was successfully sent."
Therefore, it is required first of all to learn how to receive data from a form in a script.
In PHP, this is easy - all the data from the form is in a global associative array $_POST
. This array will always be implicitly present in the script if it was loaded using the POST method.
Each field from the form will be in an array, where the key will be the value of the name attribute, and the value will be the content of the field. For example, to display all the information from a form on the screen, you can write the following script:
<?php
if (isset($_POST)) {
print("Name: " . $_POST['name']);
print("<br>Email: " . $_POST['email']);
print("<br>Message: " . $_POST['message']);
}
?>
The function isset
is used to determine if the variable passed to it exists. This is how we check that the script was loaded using the POST method, that is, the form was submitted.
Typically, after processing a form in PHP, the script should redirect the user to another page. This is due to the fact that if the form was submitted via the POST method, then after the page is refreshed, the data will be sent again, and this, in most cases, is undesirable behavior.
Sending files
In addition to text information, it is possible to send files of any type to the server. An example of a file upload form:
<form name="file_upload" method="POST" action="form.php" enctype="multipart/form-data">
<label>Avatar Image: <input type="file" name="avatar"></label>
<input type="submit" name="send" value="Upload">
</form>
There are two important differences from the first example:
- A new enctype attribute has been added, which should always have a value of
multipart/form-data
. If it is not there, then the file will not be sent. - The file itself is loaded using a field of type file .
In PHP, the uploaded file will be available in another special array - $_FILES
.
<?php
if (isset($_FILES['avatar'])) {
$file = $_FILES['avatar'];
print("Uploaded a file named " . $file['name'] . " and size " . $file['size'] . " byte");
}
?>
PHP automatically saves all uploaded files to a temporary folder on the server. But you cannot store files there, because this directory is periodically cleared, and a link to such a file cannot be given on the site. There is only one solution here - move the downloaded file to another folder. File transfer is always performed immediately after downloading.
First you need to make sure that there is a folder in the working directory of the project to store the uploaded files. Let it be called uploads
.
Moving an uploaded file
To move a file, you need to know where it is now and the address of the folder where it will be transferred.
With the current address, everything is extremely simple - it is already in the $_FILES
. The new file address, in turn, consists of the path to the folder and the file name. Since the uploads folder is located in the same place as the current script, you can get the path to it like this: dirname(**__FILE__**)
.
Code to move file to new folder:
<?php
$current_path = $_FILES['avatar']['tmp_name'];
$filename = $_FILES['avatar']['name'];
$new_path = dirname(**__FILE__**) . '/' . $filename;
move_uploaded_file($current_path, $new_path);
?>
The function move_uploaded_file()
does two things:
- Checks that the file is indeed uploaded via the form.
- Moves the downloaded file to a new location.
Form validation
Form validation is the validation of the contents of its fields. The task of such a check is to make sure that the required fields are filled in and the values in them correspond to the expected format.
So, for example, when registering a user on the site, he must fill in the fields with an email address and come up with a password. Both fields are required, but the value from the email field must also be a valid email address.
In addition to form text values, you can check the format and size of uploaded files.
General Approach to Validation
When validating any form, the procedure will always be the same:
- Form an array with the names of fields that must be filled in.
- Form an array with rules for field format validation.
- Get the values of all fields.