Working with forms in PHP
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.
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
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
This array will always be implicitly present in the script if it was loaded by the method
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:
<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 -
Code to move file to new folder:
General Approach to Validation
When performing validation of any form, the order of actions will always be the same:
- Form an array with the names of the required fields.
- Form an array with rules for field format validation.
- Get the values of all fields.