Working with forms in PHP

Working with forms in PHP

Forms

Forms are part of the HTML language. Forms are needed to transfer data from client to server. Most often, forms are used to register users, fill out questionnaires, place an order in an online store, and so on.
Both simple text information and files can be sent through forms.
Most of the time you are programming in PHP, you will be working with forms and data from them in one way or another.

HTML describes what elements a form is made of and what it looks like. But without a receiving side, that is, a server that receives this data and processes it as needed, there is no point in creating forms.

PHP contains many tools for working with forms. This makes it very easy to solve typical problems that often arise in web programming:

  • User registration and authentication;
  • Posting comments on forums and social networks;
  • Registration of orders.

Almost any modern site contains at least several different HTML forms.

Submitting the form

Let's consider one typical example - a feedback form. To contact users with the authors of the site, as a rule, feedback forms are used, where a person indicates a name, mail 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>User Name: <input type="text" name="name"></label>
  <label>User email: <input type="text" name="email"></label>
  <label>Comments: <textarea name="message"></textarea></label>

  <input type="submit" name="send" value="Send">
</form>

It is a very simple form with 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 two tag attributes 

that are needed to indicate how the data is processed:

  • method- This attribute is used to define the HTTP method that will be used to transfer data to the server. You are already familiar with the HTTP method GET, which tells the server to simply return a specific document.
    The method POSTinforms about the intention to transmit some information to the server, which, however, does not cancel the subsequent receipt of the content.
  • action - contains the address of the PHP script that should process this form.

After clicking on the "send" button, the browser makes a POST request with the entered data to the address specified in the attribute action.

Form processing

After submitting the form, control is passed to the PHP script, which must receive the submitted data, perform some action with it (for example, save it to the database) and display the result.
The result can be some kind of message about the successful completion of the operation, for example, "your data was sent successfully . "

Therefore, it is required first of all to learn how to get data from the form in the script.
PHP makes it easy - all data from a form is in a global associative array$_POST .
This array will always be implicitly present in the script if it was loaded by the method POST.
Each field from the form will be in an array, where the key will be the value of the attribute name, and the value will be the content of the field.
For example, to display all the information on the screen from a form, you could write a script like this:

An example of a form for uploading a file:
<form name="file_upload" method="POST" action="form.php" enctype="multipart/form-data">
  <label>Ваш аватар: <input type="file" name="avatar"></label>
  <input type="submit" name="send" value="Send">
</form>

There are two important differences from the first example:

  • A new attribute enctypehas been added , which should always have a value multipart/form-data. If it is not there, the file will not be sent.
  • The file itself is loaded using a field with the "file" type.

In PHP, the uploaded file will be available in another special array - $_FILES.

With the current address, everything is extremely simple - it is already in the array $_FILES. The new file address, in turn, consists of the path to the folder and the file name. Since the folder uploadsis in the same place and the current scenario, to get a path to it so you can: dirname(__FILE__).

Code to move file to new folder:

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 field emailmust also be a valid email address.
In addition to the text values ​​of the form, you can check the format and size of the uploaded files.

General Approach to Validation

When performing validation of any form, the order of actions will always be the same:

  1. Form an array with the names of the required fields.
  2. Form an array with rules for field format validation.
  3. Get the values ​​of all fields.