PHP form processing: concept, processing instructions, rules and working methods

Forms play an important role in designing web resources. They allow you to enter and change data. Malicious code or knowingly false information may come through forms. Creating the correct forms and their correct functioning is comfort for the visitor and stable operation of the web resource.

HTML forms and real tasks

HTML forms are present on every site with a few exceptions. Using the form tag, you can provide the visitor with the ability to enter and modify data. The most popular tag - input - is used to enter strings, passwords and transfer hidden data. The select tag allows you to select an item from a list. You can also set switches, make multiple position selections, and transfer files.

A small number of internal form tags, a variety of their attributes and applications makes it possible to describe complex data structures.

The concept, purpose and actual processing of a form in PHP are determined by the task at hand. Designing a form is a complex process, and providing a complete solution to the task takes a lot of time: the external simplicity of a form tag in practice is quite laborious to implement.

All form elements can be styled with CSS styles; JavaScript event handlers can be assigned to them. The programmer can provide the visitor with an algorithm for moving through the fields of the form.

Real form example




In fact, the concept of form depends on the subject area. For example, for the accounting website, forms of primary documents, reports for tax, payroll sheets will be created ... The relationship between the form and the real document is determined by the purpose of the latter.





In particular, the input of the primary document is:

  • name;
  • quantity;
  • cost of goods.

The remaining fields (supplier, delivery date, units, legal details, etc.) can be filled in automatically as part of the auto-complete fields algorithm - a modern and popular programming technique.

The payroll sheet may require entering (correction) of only one field - the accrued amount (if it needs to be clarified outside the main calculation). The remaining fields are calculated according to a single algorithm for all employees.

In each case, the form contains only those elements that must be filled out or modified.

An example of using form elements

In the given example, a text field was used to enter the visitor’s name, a hidden field was used to enter the password, a list item and two options:

  • one of many (radio);
  • several of many (checkbox).

This form is formal. Its purpose is to show how easy it is to create the ability to enter / change data on any site. At the same time, a hidden element is present in the form, which is not introduced or changed, but allows the transfer of important information.

Form Examples




HTML forms are self-contained elements and work on their own. Correctly describing and using them in practice is very difficult. As a rule, a change in form (adding or removing fields) is associated with multiple changes in the code.





It is important to immediately determine the content and number of fields needed so that there is no need to change or supplement subsequently.

PHP form description code

The given form example is described by the following code. CSS descriptions are not significant and are not given below. Only the code of the form itself is shown. Here PHP sends the form data using the POST method to the 'index.php' page, on which the form itself is located.

Form Description Code




The fields for the visitor’s name and login password must be filled in correctly. The possibility of entering false information should be excluded initially. To do this, the form code provides for the use of changing the color of field characters to red in case of an error: echo $ cNameTest and echo $ cPassTest. These variables are defined at the beginning of the HTML page.

Validation of form fields




Here, in the HEAD (1) section, operators are inserted that determine whether the cName and cPass form field values ​​are in the $ _POST array. Then, the correct input of the necessary values ​​is checked: functions (2) and (3). If the user made a mistake and entered the name or password incorrectly, then the corresponding form fields change color to red.

The verification functions chStrText () and chStrPass () are not ideal - this is an example. In real practice, it is important not only to change the color and show the visitor his mistakes, but also to take adequate actions.

Of particular note is the input tag of type hidden (cCode name). Processing form data in PHP is not only visible or fillable information. Often during the data entry process it is necessary to generate hidden text that matters. In this case, it does not matter what the programmer puts in the cCode value; it is important what matters for the solution of the task.

HTML to PHP Ratio

PHP is running on the server. As a server language, PHP can only take “formative” part in form processing: the programmer writes code to create the form or fill out its fields when the form is described by HTML tags.

PHP can take a decisive part when it is necessary to process the data entered by the visitor. PHP is activated when the page is refreshed: after filling out the form, it sends the data to itself using the POST method (sometimes, the GET method). That is, the value of the action attribute of the form tag is the name of the page on which the form is written.

In some cases, the rule for processing a form in PHP requires a separate page, sometimes invisible. This is a very convenient method. For example, registering or logging in as a registered visitor is associated with a database request.

Technology Stack and Form




In this case, three pages are developed (as an option). The source index.php file (index.html) contains the login (registration) form. CheckUser.php verification file is a ready-made form in PHP for working with the database (user table) and more. CheckUser.php only checks for the presence of a visitor; it does not output to the browser.

In case of successful login, the main work page is loaded, and the user logs on to the site. In case of problems: there is no such user or registration is refused, and the process returns to the index.php (index.html) page.

Work with the server on AJAX

In some cases, processing the form in PHP through page refresh is the only option. When a user logs on to the site and starts working, it is advisable to check the form fields on the fly using JavaScript. Assigning a handler to events of form fields (entering a field, exiting a field, pressing a key when the focus is in the field), it is possible to generate data for subsequent processing. After the data is prepared, and the visitor clicks on the data transfer button.

Using Forms and AJAX




This example has a login form. And there are two HTML elements that are not related to the form in any way, but clicking on them causes the launch of a function that hides the form or function that reads the fields and sends them to the server for verification.

In this case, the task was set to provide login from one browser to different employees without rebooting the resource. The resource must work constantly, and employees are changing. Using a PHP and AJAX form is the most convenient solution.

HTML form validation

A site visitor may be mistaken, or may knowingly try to do harm. In any case, the developer must provide all the options for the visitor. The simplest rule when creating forms is to allow input of only valid information.

The use of regular expressions is not always justified, as in the example with the chStrTest () and chStrPass () functions, but the requirement to accurately establish sets of valid characters when entering / changing data fields or a likely set of values ​​is always justified.

Form validation




Ideal is to provide the visitor with the opportunity to fill out the form through lists of valid values, but in most cases this is simply impossible.

It is possible to execute HTML form processing directly at the time of input in PHP, but this will put a strain on data exchange through AJAX. The practice and experience in developing forms allows us to conclude: a reasonable combination of JavaScript features (checking data input in a browser) and PHP capabilities (processing data on a server) is the best solution.

Rigid designs and laboriousness

HTML is a "set" of formal rules. Tag descriptions require CSS tables. By describing form elements through identifiers, you may lose the ability to directly access form fields through JavaScript. Using the description through classes, you can have more freedom. It is practically impossible to give clear instructions, all situations are unique, but understanding and using the skills and knowledge of colleagues always allows you to quickly and efficiently complete the work.

Forms, JS & PHP




Creating a dynamically forming form for data entry is difficult. Typically, working with forms in PHP comes down to formalizing data and forming a rigid structure in the server language or HTML tags.

In any case, the solution should be built as conveniently as possible for possible change. The description, validation, and processing of the form are recorded in different parts of the page, CSS files are used, and object description files can be used.

In all cases, the description and processing of the form in PHP should be commented and formulated as much as possible. If you foresee an option to change the number of fields, design, placement and processing, this will make the maintenance of the work done simple and quick.

Always only one form

The process of using forms has one cardinal drawback: “it is always necessary” to have two variants of the same form.

Always only one form




The first option is data entry / change. The second option is a form after input validation. Even when the functionality of checking each field on the fly is implemented in JavaScript, processing the form in PHP can give a negative result and you will need to invite the visitor to correct the incorrectly specified data.

It will not work to return the data back to the form, because you need to show the visitor what needs to be fixed, which means that the original form must be presented in a slightly different version.

The unspoken general rule of programming - “everything is always exact in one place and in one copy” has the most direct relation to forms. Any form should be created universal, with the expectation that errors were detected during its processing. It's just that at the initial moment of time they are not in form.

Only in this case there will be no provision during the initial data entry and re-entry - after fixing the errors.

Rules and methods of working with the form

Any practical task was solved before the creation of the site. Web resource development and designing the necessary set of forms is a formalization of the information processing process that existed before the programmer began to develop.

From this point of view, when creating any form, the human factor is important. The location, functionality and operation of the form should be absolutely intuitive to any visitor in general or to a specialist if it comes to creating a professional specialized resource.

The human factor is the first criterion. The simplicity of the form and its processing is the second essential aspect. A unique form code in one copy, designed for initial input and for correcting errors made is the third criterion.

Natural data and formal description

The situation with forms has always been characterized by extreme simplicity in using HTML tags and real difficulties in their application in practice. Natural information is dynamic, any subject area and its functioning do not stand still.

The more massive the HTML form, the more difficult it is to create, use and process in PHP. Laboriousness sometimes rolls over when designing forms, and especially when changing, testing and developing them.

Knowledge and skills improve the situation and allow you to create good, high-quality forms for data entry. Observing the most elementary rule: maximum simplicity and minimum fields, you can create good code and ensure high-quality data processing.




All Articles