POST and GET requests with simple words. How to send POST Request Browser: Method Post Variable Post in PHP Use Examples
In this lesson, we consider receiving data transmission between forms and pages. These methods are POST and GET. We will talk about each other separately and in more detail. Generally speaking, it is necessary for communication between forms. For example, we fill out some fields on the page and we need to transfer them to another page for processing.
GET method in PHP
To begin with, we will analyze the GET method. This is when all variables and their values \u200b\u200bare transmitted directly through the address. Now you will see everything on the example, and even understand how most sites and forums work. For example, we have an HTML page of this type:
Page with an example of transmitting variables using Get
link
See the link? It is complicated and consists of several parts. Let's wonder everything around the shelves: https: // Site - The address of the domain or, as it is also called, Host. index.php. - PHP page that will process the request. ?
- The separation symbol between the address and block with variables. Next are variables and their values \u200b\u200bthat are separated by the symbol. &
. name \u003d Sergey - Name variable and its SERGEY value. age \u003d 22. - The same, variable AGE, value 22.
All disassembled, now let's see how it is processed in PHP, using the GET method. Page index.php, as you remember, we passed to her:
To begin with, advice: always check the variables on the correctness: on the void, for compliance with the valid values \u200b\u200band so on. Since everything is transmitted via the address bar, the data can be easily replaced and hurt the site. Now according to the code itself: we, with help, checked the variables of the name and age to the emptiness and, if they are not empty, then brought them off, and if empty, then simply reported it. Everything is simple, agree? For example, you can create an HTML page and make links through variables in the index.php to index.php and display this or that page depending on the resulting value. Well, we will talk about it later, in the article about creating a website on PHP from scratch. To miss anything, I advise you to subscribe to RSS.
PHP PHP method
To demonstrate the work of this method, we will need a little more than a simple line with the address :) It will be necessary to create an HTML page with a filling form, but nothing, I will give a ready-made example for you:
Page with an example of transmitting variables using POST
So, we have created an HTML page with a simple form. Remember, the POST method can only be used in the form. The first form parameter is "Method", it defines the method that we will use for transmission. As you could guess, it is either GET or POST. In this case, if GET is installed, then all the names of the fields (in the form of variable names), as well as their values \u200b\u200bare transmitted by reference, as in the section about the GET method. If POST is set, then all the names of variables and values \u200b\u200bwill be transmitted as a browser request to a web server. That is, they will not be visible in the address bar. In many cases, this is very useful. Also, POST is safer, it is also clear, because the variables with their values \u200b\u200bare no longer so easy to edit, although it is also possible.
The second parameter of the form is "Action". This is the path and name of the script file to which we transmit data. In our case, this is index.php. This path can be transmitted and completely, that is, so: action \u003d "https://my_site.ru/index.php". If you do not specify the value of the "Action" parameter, then all information will be transmitted to the main script, that is, the index.php index page of your site, which is quite logical.
Now we get data from our form. Once we passed in index.php, it means that the code will be the code of this page:
"; Echo" Name - "; echo $ _post [" user_name "]; echo" age - "; Echo $ _Post [" Age "]; echo" years ";) ELSE (ECHO" Variables did not reach. Check all times. ";)?\u003e
Do not forget to check for emptiness and permissible values. Next, you need to clarify why our variables are called the User_Name and Age? And you look at the shape fields that we created above. See there iNPUT NAME \u003d "User_Name" Type \u003d "Text" ? Here is the NAME parameter and sets the name of the variable that we get with this field. The same with AGE. I hope it is clear. Well, the acquisition of the variable and its values \u200b\u200bvia POST is almost no different from the GET, which we considered above.
Well, the lesson turned out to be big, but one of the most useful, because the transfer of variables between forms and pages is exactly the interactivity for which we use PHP.
You could notice that the addresses can be contemplated on most sites:
Http: //syt/index.php? Blog \u003d 2
Here, not even knowing PHP, you can guess that we appeal to the file index.php. But what goes after the question of the question, few people know. Everything is quite simple: ? blog \u003d 2 This is an announcement of the global variable "$ _Get [" Blog "]" with the value "2". Thus, I transmit a variable to the script that is responsible for output from the database. Let's write a small script in which you will only clearly see:
if (isset ($ _ get ["blog"])) ( echo $ _get ["blog"]; } ?>
We use the IF () condition operator () as a condition worth this line:
Isset ($ _ get ["blog"])
isset () Allows you to find out if there is a variable that is listed in brackets, that is, the condition that I described in the code sounds like this: if there is a variable $ _Get ["blog"] then output the contents of this variable on the screen. That's what happened:
I think the global variable is clearly created. $ _Get. with the identifier that we have announced in the address bar ( in this case, with the "Blog" identifier)
Now I want to clarify one moment. Suppose we need to declare two variables, how to do it? The first variable is declared after the question of the question "?" The second variable is declared after such a sign "&" ( To be honest I do not know what kind of sign ), here is an example announcement of three variables:
Http: //syt/index.php? A \u003d 1 & b \u003d 2 & c \u003d 3
Here is the output code:
if (ISset ($ _ get ["a"]) and isset ($ _ get ["b"]) and isset ($ _ get ["C"])) ( Echo $ _GET ["A"]. " "; Echo $ _get ["b"]. " "; Echo $ _Get ["C"]. " "; } ?>
The condition sounds like this:
If there is a global variable $ _GET ["A"] and the global variable $ _Get ["b"] and the global variable $ _Get ["C"] then output them on the screen, Here is the result:
Forms
Before we go to pOST. Requests, you need to disassemble what form are? Why do you need? Because the global variable $ _post ["] is created through forms. What is a form? These are fields to enter any information of the user. Fields are in one line, large fields, there are radio buttons, check boxes. We will analyze everything in order ...
Form is a tag:
The form has attributes, list the most common:
Let's create a form:
I set the file as a handler file test.php. Since it is in it that I am writing examples for you. I put the POST method as it is these methods that use in 99.9% of cases. I also assigned our form name - Form
Now plunge into the world of form elements. First of all, you must understand that almost all elements are tag Difference only in the attribute type These tags. Let me list the forms you used:
I am sure that you have not met such fields, so here as it says: "no comments"
Now let's make a small training questionnaire with which we will work further. Our task is to make a small form that tells us the name of the fill, the floor, from what country it is, favorite color and field of text where the user can add something about himself. That's what I did:
If the user enters some text to the text field and clicks on the "Submit" button, then the TEXT variable will be sent to the server with the content of the content that the user entered.
POST and GET requests with simple words
This variable will be sent by the POST method.
If you write in the form:
Did not find an answer to your question? Look at here