Outputting form submitted data to php. Creation of HTML forms. II. Entering data through a loop
To organize the transfer of data to the server using a form, you will need to implement an HTML form into which site visitors will enter their information and PHP code, the purpose of which is to receive and process the received data on the server.
HTML form for submitting data
The form on the page is formed by tags
, inside which the tags of the input fields are placed text information, tags for special components (for example, a combo box), tags for a select box, and file upload.* For HTML5, it is also possible to place form field tags not inside form tags, but anywhere on the page. At the same time, for each such field, you need to specify the "form" attribute to determine which form of submission it should interact with.
So, simplest form sending may contain the following code:
Form elements and their parameters:
action = "myform.php"- the "action" attribute defines which php file will process the sent data. In this example, the data will be sent to the file "myform.php" located in the same directory as the page with the form. If you do not explicitly specify this attribute, the form data will be sent to the page address of the form itself.
method = "post"- the method parameter defines the method of data transmission POST or GET. More details on this in the article "Differences between POST and GET methods". If you do not specify the attribute explicitly, the GET method will be used by default.
Text "Meaning A:" and "Value B:" added only for the purpose of design and clarity of the form for the user. It is not necessary to add this for data transfer, but in order for the user to understand what to enter, it is worth specifying.
Tags are used to form various form controls.
type = "text"- the "type" attribute defines the type of the field. Depending on which type is specified, changes and appearance element, and its purpose. The value of the "text" attribute indicates that the browser will render the element as a single-line text field where the user can enter their string.
name = "data1"- the "name" attribute, indicates the name, or rather the index of the data in the array received by the server. This is a required parameter, by which the passed value can then be accessed in the php handler. The name can be chosen arbitrarily, however, it is more convenient when this value has some understandable meaning.
type = "submit"- tag with this value of the parameter "type" will be displayed on the page as a button. In fact, you can do without a button on a form. If, for example, the form has text fields, then sending can be done by simply pressing "Enter" on the keyboard. But the presence of the button makes the form more understandable.
value = "(! LANG: Send" !}- in this case (for type = "submit") only defines the caption on the button. For type = "text", for example, this will be the text that will be displayed in the text field.
As a result, on the page this code will look something like this:
By clicking on the button, the data will be sent to the specified page, and if it exists and works correctly, the data will be processed.
Processing HTML Form Submitted Data in PHP
The data sent in this way is placed in the $ _POST, $ _GET and $ _REQUEST superglobal arrays. $ _POST or $ _GET will contain data depending on which method was used to send. $ _REQUEST contains the submitted data by any of the specified methods.
$ _POST, $ _GET and $ _REQUEST are associative arrays whose index fields match the "name" attributes of the tags ... Accordingly, to work with data in the myform.php file, you can assign variables to the values of the elements of such an array by specifying the field name as an index:
// for the GET method
$ a = $ _GET [ "data1"];
$ b = $ _GET [ "data2"];
// for the POST method
$ a = $ _POST [ "data1"];
$ b = $ _POST [ "data2"];
// for any method
$ a = $ _REQUEST [ "data1"];
$ b = $ _REQUEST [ "data2"];
Checking the completion of form fields
Sometimes, when receiving data, you need to check if the user has submitted an empty form. You can use the empty function for this.
if (empty ($ _REQUEST ["data1"])) (
echo "The field is not filled";
} else (
echo "The field has been filled in";
$ a = $ _REQUEST [ "data1"];
}
Usually this solution is sufficient. If you need to enter text, it will be clear whether it is entered or not. However, if the user deliberately enters zero for calculations, then the empty function will show that there is no value. Therefore, it is better to use the isset function for such situations. It will explicitly check if the value is given or not.
if (isset ($ _REQUEST ["data1"])) (
echo "The field has been filled in";
$ a = $ _REQUEST [ "data1"];
} else (
echo "The field is not filled";
}
or how to send a message by e-mail using HTML forms
You have your own website and want to receive letters or messages from your users, questions, advice or just wishes about e-mail then this tutorial is for you!
How to send a message by e-mail
We need the following files:
- form.html - a page with a form.
- form_processing.php - script file, processing HTML form.
Consider the form.html code:
|