What a man needs post asp method. Using the GET and POST Methods

So we continue our study PHP fundamentals and in this article we will get acquainted with the ways of passing variables in PHP, namely with GET and POST methods. Each of them has its pros and cons, and is used in appropriate situations, which will be discussed in this article. We'll also look at code examples that demonstrate how the POST and GET methods work.

Passing variables using the GET method

This method of passing variables is used in PHP to pass variables to a file using the address bar. That is, the variables are transmitted immediately through the address bar of the browser. An example would be, for example, a link to an article in WordPress without the use of CNC (SEF), which looks something like this:

https://website/?p=315

That is, in this case, the $p variable with the value 315 is passed. Now let's take a closer look at the GET method using an example. Let's say we need to pass three variables $a, $b and $c to the file GET method and display their sum on the screen. You can use the following code for this.

$a = $_GET["a"]; $b = $_GET["b"]; $c = $_GET["c"]; $summa = $a + $b + $c; echo "Sum of $a + $b + $c = $summa";

Since all variables will be placed in before passing global array GET(), then we first assign our variable values corresponding elements of the GET array. We do this at the very beginning in order to avoid various errors when passing variables. Next, to demonstrate the work, we write an arbitrary formula and display the result on the screen.

To test the operation of the GET method, simply add a question mark "?" and through the ampersand "&" list the variables with their values. Let's have a file get.php, which lies in the root of the site. In order to transfer variables to a file, it is enough to write the following in the address bar.

https://website/get.php?a=1&b=2&c=3

As you can see from the example, first we add a question mark right after the file name. Next, we prescribe a variable and specify its value through equals. After that, through the ampersand, we list other variables in the same way. Now, when we click on this link, we will see the sum of the variables $a, $b, and $c.

This method is very simple and does not require additional files. All the necessary data comes directly through the address bar of the browser.

Well, now let's move on to the second way of passing variables in PHP - POST method.

Passing Variables in PHP Using the POST Method

This method allows you to covertly transfer variables from one file to another. As you already understood, two files are usually used for these purposes. The first contains a form for entering initial data, and the second contains an executable file that accepts variables. To demonstrate, let's look at the following code.

The code of the first file with the form for submitting data. Let's name it post-1.php

  • action - specify the file to which the variables will be transferred.
  • method - method for passing variables. In our case, this is the POST method.
  • name - the name of the form. At the same time, a variable with that name will be transferred to the file.

Text fields:

  • name - variable names. In our case, this is the name and surname (variables name and lastname).
  • type – field type. In our case, this is a text field.
  • name - the name of the button and the variable that will be passed along with other variables.
  • type - button type. In our case, this is a button for submitting data.
  • value - text on the button.

The code of the second file, which will serve as a receiver of variables. Let's call it post-2.php

$name = $_POST; $lastname = $_POST; echo "Values ​​of variables passed by POST method - $name and $lastname";

As with the GET method, we first assign the values ​​of the corresponding elements to the variables global arrayPOST. Further, for clarity, we display these variables on the screen using .

Now, when the first file is loaded, the form will load. After entering the data, click on the "Submit" button, as a result of which a page with the second file will open in a new tab, on which the values ​​\u200b\u200bwritten in the form on the previous page will be displayed. That is, the values ​​of the variables from the first file will be transferred to the second file.

This concludes this article on passing variables in PHP. If you do not want to miss the appearance of other articles on the blog, I recommend subscribing to the newsletter in any convenient way in the "Subscription" item or using the form below.

That's all. Good luck and success in mastering the basics of PHP.

Modern web resources not only provide information to the visitor, but also interact with him. To interact with the user, you need to receive some information from him. There are several methods to get data, very common methods are GET and POST. And accordingly in PHP there is support for these data transfer methods GET and POST. Let's see how these methods work.
GET Method Data GET method are passed by adding them to the URL of the called script to process the received information. To explain this method, type the URL of the resource in the address bar of your browser and first add a question mark (? ) and then the line num=10 . For example

http://domain.ru/script.php?num=10


If you have local server, then usually the domain will be localhost , in which case the previous entry will look like

http://localhost/script.php?num=10


In this case, we are passing the num parameter equal to 10. To add the following parameters to the script, you need to use the ampersand (& ) separator, for example

http://domain.ru/script.php?num=10&type=new&v=text


In this case, we passed three parameters to the script: num with a value of 10, type with a value of "new" and v with a value of "text".
To get these parameters in the script, you need to use the built-in array $_GET $_GET["num"], $_GET["type"],$_GET["v"]. These array elements will contain the values ​​of the passed parameters. To demonstrate this example, create a script.php file with the following content



Validating the GET method in PHP


echo ($_GET["num"]."
");
echo ($_GET["type"]."
");
echo ($_GET["v"]);
?>


And now call this file in browser

http://path/script.php?num=10&type=new&v=text


and you will see the passed parameters in the browser window. But if you call this file without additional parameters http://path/script.php , you will see errors that the interpreter will throw PHP, that there are no such elements in the $_GET array. More than one article can be devoted to checking the data received from the user, so in this article I will not touch on this point.
As you probably understand, forcing the user to type data in the address bar of the browser is not very good and not at all inconvenient. Therefore, to receive data from the user, you need to use html forms. Let's write a simple html form.


Enter the number

Do you have a computer?

Your comment:





I will comment on the created form a little. Forms are created with the form tag. Form fields are created by input , select , textarea tags (you can read more). In the form tag, the action attribute specifies the URL of the script that will receive the form data. In our case, we have specified the script.php file that we already have. The method attribute specifies the method for sending data. We have specified a method GET. Now we know to which file the form data will be transferred, and in what way, it remains to figure out where to look for them there ?!
This form data will be passed to the web resource by the browser by appending it to the URL: first there will be a question mark (? ), then the parameters will be presented separated by an ampersand (& ). The name of the parameter will be taken from the name attribute, which must be specified for any form field. The value of the parameter will depend on the field type. If the field is a text field, then the value will be the text entered by the user. If the field is a list, a group of radio buttons or checkboxes, then the value of the parameter will be the value of the value attribute of the selected element. Let me explain with an example of our form. If the user enters the number 10 in the input field, then the name of the parameter will be num (the value of the input tag's name attribute), and the value will be 10 (the number entered by the user). Accordingly, the browser will generate a pair of "num=10 ". If the user selects "Yes" from the list, then the name of the option will be type (the value of the name attribute of the select tag), and the value will be yes (the value of the value attribute of the option tag). Accordingly, the browser will form a pair of "type=yes ".
Now we will place this form on the page forma.php .



Form for data transfer by GET method and PHP



Enter the number

Do you have a computer?

Your comment:







Enter any values ​​in the form fields and click the "Submit" button. After clicking the button, the browser will open another page (script.php ) and the data you entered will be displayed in the browser window. I think it's clear why: the browser will pass the data to the script.php script, and in the script this data will be processed and displayed on the screen.
POST Method Now let's see how the method works POST.
To send data by method POST You need to use HTML forms. As we remember, the method attribute of the form tag is responsible for the method of submitting form data. Therefore, you need to specify the POST value in the method attribute of the form tag. The rest of the form can be the same as for the GET method. Let's change our form, which we already used to send data using the GET method, to send it using the POST method.


Enter the number

Do you have a computer?

Your comment:





As you can see, the form remains the same except for the method and action attributes. The data will now be passed to the script_post.php script. Let's place our form in the forma_post.php page.



Form for data transfer by POST method and PHP



Enter the number

Do you have a computer?

Your comment:







Now we need to write a script that will process our form data.
To receive data in the script by the passed method POST need to use built-in array $_POST. The keys of this array will be the names of the parameters. In our case, we need to use $_POST["num"], $_POST["type"],$_POST["v"]. These array elements will contain the values ​​of the transferred data. As you can see, the difference from using the GET method is expressed only in using the $_POST array. Therefore, it will not be difficult for us to write a script_post.php file:



Checking the POST method in PHP


echo ($_POST["num"]."
");
echo ($_POST["type"]."
");
echo ($_POST["v"]);
?>


Now open the forma_post.php file in a browser. Enter some data in the form fields and click the "Submit" button. By now, you probably noticed the difference between the POST method and the GET method - the form data did not appear in the address bar of the browser. Data method POST cannot be passed through the address bar of the browser. This essential difference must be remembered.
AT PHP regardless of how the data was sent - using the POST method or the GET method - you can get the data using the $_REQUEST array. Comparison of GET and POST Methods When using the GET method, data is transferred by appending to the URL. Thus, they will be visible to the user, which is not always good from a security point of view. Also, the maximum amount of transmitted data will depend on the browser - on the maximum allowed number of characters in the browser's address bar.
When using the POST method, the data will not be visible to the user (not displayed in the address bar of the browser). And therefore they are more secure, and, consequently, the program that processes this data is more secure in terms of security. Also, the amount of data transferred is practically unlimited.
When choosing a data transfer method, you need to take into account the above features and stop at the most appropriate method.

When developing any project, one of the most basic things is to communicate with the user. We can ask him something, and give him the right to answer (survey), we can give him the right to write his opinion about a product or service, we can help him calculate the cost of our services, if you need to create an online store, etc. .

In all cases, the user must have the right to write something and click on something. That's what forms are for.

Working with Forms in PHP and HTML

Forms are denoted by the form tag:

method attribute Specifies the method by which the form will be submitted. There are 2 methods - GET and POST. Read more about this below, while in brief we will talk about the main elements of the form:

Text field - Input text

A text field in which information can be entered. name attribute- text field name, value- meaning. They look like this:

Button - Input Submit

Button used to submit forms. It looks like this:

Text block - Textarea

Text block, usually used for writing large messages. rows attribute, cols - length and width.

Appearance:

Read more about forms on our website, or in other sources. The purpose of this lesson is to show how process forms using PHP.

GET and POST method when submitting forms

GET and POST Methods are specified in the method attribute of the form when it is initialized:

GET Method transmits information in the page address, POST method- in the headers. To better understand what is GET and POST method Let's write a script that will send our name and message to the server, and the server will display the message on the screen.

AT PHP data received by the GET method stored in a variable $_GET. Data passed by POST method stored in a variable $_POST.

Let's write the code for working with the GET method first, then the POST method.

Forms - Design Studio OX2.ru

 

print_r($_GET); //Display the array with the print_r function
?>

Your name:
Message:

Now let's rewrite the script to send data using the POST method.

Forms - Design Studio OX2.ru

 

print_r($_POST); //Display the array with the print_r function
?>

Your name:
Message:

To better understand how to work with forms in PHP, sending and receiving data POST and GET methods, in the next lesson we will make a simple logic game.

When developing any project, one of the most basic things is communication with the user. We can ask him something, and give him the right to answer (survey), we can give him the right to write his opinion about a product or service, we can help him calculate the cost of our services, if you need to create an online store, etc. .

OX2 2014-10-05 2014-10-05

Today I wanted to hit on primitive things a little and describe what can be found on the worldwide web in large quantities and without much difficulty. It's practically about the holy of holies of the HTTP protocol: POST and GET requests.

Many will ask why? I will answer briefly and clearly: not everyone knows what it is and why it is needed, and those who want to know about it (while understanding little about it) often cannot understand what they write in many, many articles on this topic. I will try to explain on my fingers what POST and GET requests are and what they are eaten with.
So, let's start our journey into a fairy tale ...
If you are reading given message, then you at least know what the Internet looks like and what a website is. Having omitted all the subtleties of the work of the World Wide Web, we will operate with such concepts as a user and a site. Like it or not, these two subjects must somehow interact with each other. For example, people communicate with each other through gestures, emotions and speech, animals make some sounds, but what happens when a person and an Internet resource “communicate”? Here we have a case of information exchange, which can be transferred to the human conversation of the "Question and Answer" plan. Moreover, both the user and the site can ask questions and answers. When we talk about a site, its questions and answers, as a rule, are always expressed in the form of a web page with one text or another. When it comes to the user, then everything happens thanks to GET and POST request am (of course not only, but we are talking about them).

Thus, we found out that the objects of our theme are necessary for "communicating" with the sites. Moreover, both GET and POST requests can be used both for “asking questions” to the site and for “answers”. How are they different? Everything is quite simple. However, to explain the differences, we will have to consider an example, for which we will take the site of the Internet store plan.
You probably often paid attention when you were looking for something in online stores that when resorting to a search using filters, the site address turned from the beautiful “http://magaazin.ru” into the terrible “http://magaazin.ru/?category=shoes&size=38”. So, everything that comes after the '?' symbol is your GET request to the site, and to be quite precise, in this case, you kind of ask the site about what it has in the “Shoes” category with sizes “38” ( given example taken from the head, in reality everything may not look so obvious). As a result, we have that we can ask questions ourselves, by indicating them in the address bar of the site. It's obvious that this method has several disadvantages. Firstly, anyone who is near the user at the computer can easily peep all the data, so it is highly undesirable to use this type of request to transfer passwords. Secondly, there is a limit on the length of the string that can be transferred from the site address field, which means that a lot of data cannot be transferred. However, the undoubted advantage of using GET requests is its ease of use and the ability to quickly query the site, which is especially useful when developing, but that's another story ...
Now let's talk about POST requests. Savvy readers may have realized that the main difference between given request from his colleague - the secrecy of the transmitted data. If we consider an online store, then a vivid example where the POST request is used is registration on the site. The site asks for your data, you fill in these data and when you click on the "Registration" button, send your answer. Moreover, this data will not be displayed externally. It is also worth noting that they can request enough a large number of information - and the POST request has no significant restrictions. Well, if you touch on the minus, then you will not quickly generate such a request. Without special skills, this is no longer enough. Although in reality everything is not so difficult, but that again is another story.
Let's sum up a little. POST and GET requests are needed to "communicate" the user and the site. They are practically the opposite of each other. The use of certain types of requests depends on the specific situation and it is extremely inconvenient to use only one type of request.

In this lesson, we will look at techniques for passing data between forms and pages. These methods are POST and GET. We will talk about each separately and in more detail. Generally speaking, it is necessary for communication between forms. For example, we fill in some fields on the page and we need to transfer them to another page for processing.

GET method in PHP

Let's start with the GET method. This is when all variables and their values ​​are passed directly through the address. Now, with an example, you will see everything, and even understand how most sites and forums work.
For example, we have an html page like this:

Page with an example of passing variables using Get link

See the link? It is complex and consists of several parts. Let's break it all down:
https://site- the address of the domain or, as it is also called, the host.
index.php- the php page that will process the request.
? - a symbol of separation between the address and the block with variables.
Next come the variables and their values, which are separated by the symbol & .
name=Sergey— variable name and its value Sergey.
age=22- the same, the variable age, value 22.

All sorted, now let's see how it is processed in php using the GET method.
The index.php page, as you remember, we passed it:

First advice: ALWAYS check variables for correctness: for emptiness, for compliance with valid values, and so on. Since everything is transmitted through the address bar, the data can be easily changed and harm the site. Now for the code itself: we, with the help of , checked the name and age variables for emptiness and, if they are not empty, then displayed them, and if empty, then simply reported it.
Everything is simple, agree? For example you can create html page and in the menu, make links through variables, and in index.php process the variable and display one or another page depending on the value received. Well, we'll talk about this later, in an article about creating a site in php from scratch. In order not to miss anything, I advise you to subscribe to RSS.

POST method in PHP

To demonstrate how this method works, we need a little more than a simple line with an address :) You will need to create an html page with a form to fill out, but that's okay, I'll give a ready-made example for you:

Page with an example of passing variables using Post

Fill in the fields for information transfer:

Enter your name:

Enter your age:

So, we have created an html page with a simple form. Remember, the POST method can only be used on a form.
The first parameter of the form is "method", it defines the method we will use to submit. As you might guess, it's either GET or POST. In this case, if GET is set, then all field names (in the form of variable names), as well as their values, are passed by reference, as in the section about the GET method. If POST is set, then all variable names and values ​​will be transmitted as a browser request to the web server. That is, they will not be visible in the address bar. In many cases this is very useful. POST is also safer, which is understandable, because variables with their values ​​​​are no longer so easy to edit, although it is also possible.

The second form parameter is "action". This is the path and filename of the script to which we are passing data. In our case, this is index.php. This path can also be passed in full, that is, like this: action="https://my_site.ru/index.php". If you do not specify the value of the “action” parameter, then all information will be transferred to the main script, that is, the index.php index page of your site, which is quite logical.

Now we will receive the data from our form. Since we passed it to index.php, then the code of this particular page will be below:

"; echo "name - "; echo $_POST["user_name"]; echo "
age - "; echo $_POST["age"]; echo " years"; ) else ( echo "Variables did not reach. Check everything again."; ) ?>

Do not forget to check for emptiness and valid values. Next, we need to clarify why our variables are called exactly user_name and age? And you look at the form fields that we created above. See there input name="user_name" type="text"? This is where the name parameter sets the name of the variable that we will receive using this field. It's the same with age. I hope it's clear. Well, getting a variable and its value via POST is almost the same as GET, which we discussed above.

Well, the lesson turned out to be big, but one of the most useful, because passing variables between forms and pages is exactly the interactivity for which we use PHP.

Internet