Form data processing is one of the most important functions of the PHP programming language (PL). The available tools allow you to extract the data entered by the user and save them into special variables, after which they can be converted and written to various databases (DB) or files.
Instructions
Step 1
Create the required form using HTML, choosing the most convenient data transfer method. A handle is used for insertion. For the successful processing of user data through PHP, it is important to specify the method and action attribute. For example:
Step 2
This HTML code indicates that the form data will be passed to the script written in the process.php file using the POST method, which allows you to pass the required variables in a hidden way for the user. An alternative to the method is GET, which transfers the desired data through the address bar. Thus, after clicking the button, the entered data will be displayed in the upper part of the browser window.
Step 3
Create the required form elements using the additional attributes value, name and type. For example, to create two fields where the user can enter their first and last names, you can write the following code:
Name:
Surname
This segment allows you to create two text fields for specifying the first and last name of the user with the names usersname and familyname, which will be used for data processing later.
Step 4
Create a new file named process.php in the same directory as the HTML document where the form data is located. To create a file, right-click on a free area of the window for displaying the contents of the directory and select "New" - "Text file", then specify the appropriate name and extension. Enter the following code:
<? php
$ username = htmlspecialchars ($ _ POST [‘usersname’]);
$ second_name = htmlspecialchars ($ _ POST [‘familyname’]);
echo “Your first name is $ username and last name is $ second_name”; ?>
Step 5
This code allows you to retrieve the necessary data that the user entered into the form. $ username is assigned the name entered in the usersname text box, which was passed through the global $ _POST array. Using the htmlspecialchars () function; extra characters are removed that the user could mistakenly or deliberately write when entering from the keyboard. After extracting the necessary data from the form into variables, you can output the received information using the echo statement. Before withdrawal, the necessary actions can also be performed that may be needed to complete a particular task. The obtained values can be processed by all functions available in PHP, which means that the programmer is not limited in the means for working with HTML form data.