Process form variables. POST in PHP

How to send and receive variables through forms with PHP, sending data through POST. How to build the forms and how to receive the values ​​using PHP’s $_POST superglobal array.

Sending and receiving variables from forms is one of the most basic behaviors that we will need to implement in web applications. This type of transfer is very useful since it allows us to interact directly with the user, producing data input to PHP applications.

The process is similar to . Firstly, we present a first page with the classic form to fill in and the variables are collected in a second page that processes them.

Basic explanation of form submission with PHP

In the process of sending and receiving data from a form there are two pages. The one with the form and the page that receives it. The page containing the form could be a normal HTML page. The second page, which receives the form, will of course have to be a PHP page.

The form has an attribute called “action” that indicates the address of the PHP page that will receive the form. Also, if we want the data to be sent by POST, we must include the “method” attribute with the value “post”. You can see it in the following HTML code.

In the forms we have to include the fields that we need. The name of the fields, defined by the name attribute of the tag, will define the name of the variable that we will receive later in PHP.

See also  Advanced Command Line FTP LFTP

In the previous text field, we have indicated name=”midato” therefore, we will receive the value with the name “midato”.

Already in the PHP page, we will receive the form field with the $_POST superglobal array. This is an associative array that is automatically created by PHP and is available anywhere in the code of the page where we receive the form. The name of the form field is the one that we will use as the index of the $_POST array, like this.

echo $_POST;

Example of sending and receiving by POST in PHP

The following code illustrates an HTML page with a form that is going to be sent to another php page.

form.html

Name

Lastname

Notice that the page where the form will be submitted is called “destination2.php”. The code for this page could look something like this:

target2.php n”; echo “Variable $lastname: ” . $_POST . “
n” ?>

$_POST

It is as we say the array through which we receive the variables sent by POST currently. But this was not always so.

As of PHP 4.1.0 you can collect form variables using the $_POST associative array, which is the same as $HTTP_POST_VARS, but shorter to type.

$HTTP_POST_VARS

In older versions of PHP $HTTP_POST_VARS was used. Remember that it is possible to compile in an array type variable the set of variables that have been sent to the script by this method from the $HTTP_POST_VARS system variable.

echo “Variable $name: ” . $HTTP_POST_VARS . “
n”;

Note: Some, but rare, configurations of PHP automatically generate form-submitted variables on the landing page. You can occasionally see it like this in some examples on the web, but it’s not really a good practice. The variables received by the form in web pages are not always automatically defined, it depends on a PHP configuration variable: register_globals, which has to be activated for it to be so. See article comments at the bottom of the page for more information.

See also  Javascript problem "Blocked a frame with origin" Obsolete framing exercise?

Note: Although we can collect variables with this associative array or directly use the variables that are defined in our page, it is safer to use $HTTP_POST_VARS for two reasons, the first that this way we make sure that this variable really comes from a form and the second, That way our code will be clearer when we read it again, because it will be specified that we are receiving this variable through a form.

Example of access restriction by age

To continue providing examples of the use of forms, we are going to create a page that shows the age of the visitor and, depending on that age, allows or not to view the content of the website. Those over 18 are allowed to view the page and minors are not.

The example is very simple and would not work as it is to be used as a real access restriction. It only helps us to know how to obtain data from a form and how to treat it to perform one or another action, depending on its value.

The form page, which we have named age.php, would look like this:

Restrict by age

Type your age:

This is a page without any PHP code, it just has a form. Let’s look at the action of the form, which is directed towards a page called edad2.php, which is the one that will receive the age data and will display one content or another depending on that value. Your code is the following:

Restrict by age

“; if ($age < 18) { echo "You can't log in"; }else{ echo "Welcome"; } ?>

We hope that this other code is not strange either. The age is simply received, using the $_POST array. Then the age is displayed and a conditional expression is executed based on the fact that the age is less than 18. In the positive case (age less than 18), a message is displayed informing that the page is not allowed to be accessed. If not (greater than or equal to 18), a welcome message is displayed.

These are the bases of sending and receiving data through forms in PHP. Next we will see other slightly more advanced practices that you will use whenever you want , because you do not know for sure how many Input elements your form will have

Loading Facebook Comments ...
Loading Disqus Comments ...