Digression register form php. PHP _SELF in the form's action attribute

One of the main advantages PHP is how it works with HTML forms. The main thing here is that each form element automatically becomes available to your PHP programs. For detailed information about using forms in PHP, read the section. Here is an example HTML form:

Example #1 Simplest form HTML

Your name:

Your age:

There is nothing special about this form. This regular form HTML without any special tags. When the user fills out the form and clicks the submit button, the action.php page will be called. This file might have something like:

Example #2 Displaying form data

Hello, .
You are old.

Example output of this program:

Hello, Sergey. You are 30 years old.

If you don't take into account the pieces of code with htmlspecialchars() and (int), the operating principle of this code should be simple and understandable. htmlspecialchars() ensures that "special" HTML characters are properly encoded so that malicious HTML or Javascript is not inserted into your page. The age field, which we know should be a number, we can simply convert to an integer, which will automatically get rid of the unwanted characters. PHP can also do this automatically using the filter extension. The $_POST["name"] and $_POST["age"] variables are automatically set for you using PHP. Previously we used the $_SERVER superglobal variable, but here we similarly use the $_POST superglobal variable, which contains all the POST data. notice, that sending method(method) of our form is POST. If we used the method GET, then our form information would be in the superglobal variable $_GET . Alternatively, you can use the $_REQUEST variable if the data source does not matter. This variable contains a mixture of GET, POST, COOKIE data.

15 years ago

According to the HTTP specification, you should use the POST method when you"re using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it"s almost always an error -- you shouldn"t be posting the same comment twice -- which is why these pages aren't bookmarked or cached.

You should use the GET method when your form is, well, getting something off the server and not actually changing anything. For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.

1 year ago

Worth clarifying:

POST is not more secure than GET.

The reasons for choosing GET vs POST involve various factors such as the intent of the request (are you "submitting" information?), the size of the request (there are limits to how long a URL can be, and GET parameters are sent in the URL), and how easily you want the Action to be shareable -- Example, Google Searches are GET because it makes it easy to copy and share the search query with someone else simply by sharing the URL.

Security is only a consideration here due to the fact that a GET is easier to share than a POST. Example: you don"t want a password to be sent by GET, because the user might share the resulting URL and inadvertently expose their password.

However, a GET and a POST are equally easy to intercept by a well-placed malicious person if you don"t deploy TLS/SSL to protect the network connection itself.

All Forms sent over HTTP (usually port 80) are insecure, and today (2017), there aren't many good reasons for a public website to not be using HTTPS (which is basically HTTP + Transport Layer Security).

As a bonus, if you use TLS you minimize the risk of your users getting code (ADs) injected into your traffic that wasn't put there by you.

This PHP tutorial covers basic concepts language: processing forms with sending a request to web pages, basic PHP control constructs when processing forms, recording data from PHP forms to file, date function in PHP date().

1. Let us remind you that all php files are tested only when Denver is running and only from the browser address bar. Run php files double click it is forbidden!

2. Launch Denver.

Exercise 1: Processing a simple form

IN this lesson PHP let's look at processing a form on an HTML page, creating PHP variables for form fields.

1. Create a shape as in fig. 3.1. It is understood that when you click on the Send order button, the data from the form will be transferred to the administrator, and the client will see the response Order processed on the screen. If you find it difficult to write a form, then implement the code below the figure and save it in the php_2 folder under the name forma_bob.html We remind you that the address of the form handler is written in the action attribute of the form tag.

Figure 3.1

2. In order for the user to receive a response after submitting the data, it is necessary to create a form handler in PHP. Create the code below and save it in the php_2 folder as zakaz.php

3. Check the functionality of the handler. To do this, run the forma_bob.html file through a browser by typing in address bar browser address http://localhost/php_2/forma_bob.html

4. Enter any numbers in the form fields and click the Submit order button. The result in Fig. 3.2.

Figure 3.2

Form Variables

The whole point of using an order form is to get the customer's order information that they entered on the keyboard. Within a PHP script, each of the form fields can be accessed as a variable that has the same name as the form field. IN PHP language The variables are easy to recognize because they begin with a dollar sign $.

You can access the contents of the tireqty field in the following ways:

$tireqty //short style

$_POST[‘tireqty’] //medium style

$HTTP_POST_VARS[‘tireqty’] //long style

We'll use the long style to reference form variables, but for ease of use we'll build short versions of the application. It's convenient and safe way data manipulation that works effectively in all systems, regardless of the selected versions and settings.

When copying the contents of one variable to another, we use the assignment operator, which is denoted in PHP using the equal sign (=). The following line of code creates a new variable called $tireqty and wraps the contents of $_POST['tireqty'] into this new variable:

$tireqty=$_POST['tireqty']

Since this script does not generate any output, it makes no difference whether it is placed above or below the . Typically this block is placed at the beginning of the script.

5. In the zakaz.php file, change the code as follows and check the functionality of the form. The result in Fig. 3.3. The numbers may differ depending on what data you entered.

Figure 3.3

Exercise 2: Create a calculator for the Bob's Auto Parts form

In this PHP lesson we will cover basic arithmetic PHP functions for calculations.

1. Save the file forma_bob.html under the name forma_bob_2.html

2. Save the file zakaz.php under the name zakaz_2.php

3. In the file forma_bob_2.html, change the form “Auto parts from Bob” so that it looks like in Fig. 3.4. Don't forget to change the handler name to zakaz_2.php. If you are at a loss, you can implement the code below.

Figure 3.4

4. Create a new form processing in the zakaz_2.php file, using the knowledge gained from previous lessons. The processing must contain the following output data and conditions:

1. The cost of each product is determined by a constant. A constant in PHP is specified by the define function. Example define("POKRPRICE",10); The first parameter of the function is the name of the constant, which is written in capital letters, the second parameter is the value of the constant.

2. The logical OR operation is denoted by ||

3. The logical AND operation is denoted by && .

4. Comparison operations: greater than >, less< , больше или равно >= , less than or equal

Switches

In this article, checkboxes are elements created by input tags with a type parameter value of checkbox . The form for using a variable number of “switches” is built in exactly the same way. Note that the choice of the specific radio button value (that is, the value of the value property) is not important. An example is shown in the listing below:






However, the processing of such a form differs from the processing described for text fields. IN in this case it is necessary to determine whether a site visitor has turned on this or that switch. If enabled, then the corresponding array element exists; if not, then it is missing. The following listing is an example PHP script which prints out the enabled radio buttons:

Radio buttons

Before describing the processing of radio buttons, it is necessary to remember how they work. The essence of radio buttons (elements created by input tags with the value of the type parameter equal to radio ) is that by selecting one button, the user automatically deselects another button from the same set. Buttons are combined into a set very simply: all buttons in the set have the same name.

But the values ​​(that is, the value parameters) of the buttons in the set are different. And the value of the selected button with the name of the set will be sent to the site. The same as in the case with text fields and radio buttons, the names of sets of radio buttons should be formatted as names of array elements in PHP. An example of such a form is given in the following listing:

// first set of buttons
// second set of buttons
// third set of buttons

Processing radio buttons combines the ideas of using both text fields and radio buttons in processing. If the author of the html page has not set a default value, and the user has not selected a specific button in the set of radio buttons, then this element will not be in the array (as for switches).

If the button is selected, then the corresponding array element will contain its value (as for text fields). Below is an example listing that processes a form with multiple sets of radio buttons.

Thus, nothing difficult to process complex shapes No.

One of the previous topics already discussed obtaining data from a form. In this topic, I will tell you in more detail how to correctly submit a form to the server, as well as process the form in PHP.

Submitting a form

Input field. The most common form elements are various input fields. They are created using tags of many types and the tag. Sending data from these elements is simple. Tags have a name attribute that specifies the name of the parameter. And the parameter value is the text that the user will write in the input field. Additionally, tags can have a value attribute that specifies a default value. This value will be sent to the server if the user does not enter anything. For example, let's create a form and add input fields to it:

1
2
3
4
5
6
7
8
9
10
11
12
13

Page

select. Data is sent from the tag like this: This tag itself has a name attribute. And the value attribute is not on the tag, but on the list items, that is, on the tags. Typically, each item has its own value attribute value. Whichever item the user selects, the parameter value is taken from such item. In this case, the text in the tag does not matter, only the attribute is used. Add a tag to the form:

12
13
14
15
16
17
18

Interests

Sports Nature Cinema Literature

Radio button. All radio buttons from the same group must have the same name attribute. This value not only sets the parameter name, but also groups the radio buttons into a group from which only one radio button can be selected. The value attribute is set differently. Let's add radio buttons to the form:

Checkbox. Unlike radio buttons, each checkbox is a separate form element. All checkboxes are independent of each other and each one sends its data to the server. name attribute all checkboxes must have a different one, and the value attribute can be either the same or different. However, the checkbox does not have any specific data. It can either be selected or not. Therefore, there is no point in setting it to a long value. It can be given a value of one character, for example, 1. On the server, the meaning of this value will be this: if there is a value, then the checkbox was selected, and if there is no value, then it is not selected. Let's add checkboxes to the form and create a submit button:

Processing data from forms

In the proposed example, a form has been created that sends data to a script called takeform.php. Therefore, you need to create this script. This will not just be a program, but a page that will be generated depending on the data received from the form. The form is sent using the POST method. The script takes this data from the $_POST superglobal array. Array elements can simply be used in a script, but if you need to work with them a lot, then writing the name of the element every time is inconvenient. It's easier to write the value into a variable and access it. Let's create a file takeform.php and write it to variable values from the first two form elements:

takeform.php:

We didn't add a value attribute to the tag. If you do not enter anything into it, the value will be empty. Often you need to check whether the user has written something in an input field. There is a function for this called empty(), which returns true if the variable contains an empty value, and returns false otherwise. On the page that accepts data from the form, we will check whether something was entered in . We wrote the value from it into the $self variable, and we will check it. If it contains a value, then we will add a block to the page and place the text from the variable in it. If the variable is empty, then the block will not be created.

13
14
15
16
17

if (!empty($self)) ( echo "

About Me:

"; echo "".$self.""; )

select. Text from input fields is usually used on its own. It is written to the database or output to the page. But data from other form elements is used slightly differently. Having received data from the form, the script does not display it, but determines based on it what actions need to be performed. Let's form part of the page depending on the user's selection of a list item:

Please note the opening tag

It comes before data processing, in line 19, and the closing one comes at the end, in line 28. Thus, not only the HTML code is divided into parts, but also a specific tag. This is common practice.

Radio button. All radio buttons in the same group have the same value attribute. Accordingly, one element of the $_POST array is created for the entire group. The value from the radio button that the user selects will be written to it. Let's add the result of selecting a radio button to the page:

Checkbox. Each checkbox is processed separately. Usually a checkbox does not contain specific data, so it is simply checked whether it contains a value or not. If there is a value, then the user selected it and this is taken into account when processing the form data. Let's add information to the page about what the user has selected:

This is how data from the forms is sent to the server and processed in PHP scripts. Depending on different tasks, working with forms is carried out differently, but based on the examples given, you can easily use the data from the forms the way you need.

This article discusses the use of the PHP _SELF variable in detail.

What is the PHP _SELF variable?

The PHP _SELF variable returns the name and path of the current file (relative to the document root). You can use this variable in the form's action attribute. There are also some nuances that you should know. Of course, we cannot ignore these nuances.

Let's look at a few examples.

Echo $_SERVER["PHP_SELF"];

1) Let's assume that your php file located at the following address:

Http://www.yourserver.com/form-action.php

In this case, the PHP _SELF variable will contain:

"/form-action.php"

2) Let's assume your php file is located at this address:

Http://www.yourserver.com/dir1/form-action.php

PHP_SELF will be:

"/dir1/form-action.php"

PHP _SELF in the form's action attribute. Why was she needed there?

Usually PHP variable _SELF is used in the action attribute of the form tag. The action attribute specifies the address to which the contents of the form will be sent after confirmation (the user clicks on the button with type="submit"). As a rule, this is the same page from which the form left.

However, if you rename the file referenced by the form, you will need to rename the file name in the action attribute, otherwise the form will not work.

The PHP _SELF variable will save you from unnecessary corrections, since the page address will be generated automatically based on the file name.

Let's say you have a form file called form-action.php and you want the form to be submitted to the same file after confirmation. Usually they write like this:

But you can use the PHP _SELF variable instead of form-action.php. In this case the code will look like: