Retrieve data from a form with PHP

Let’s create a code at PHP that allows us to retrieve the data entered in a form and show it to us on the screen.

The first step will be to create the form HTML that allows us to capture user data. This form will have two text fields and a button that allows us to send it.




At this point the value of the attributes is very important name, since the values ​​that we give to these attributes will be the ones that will help us recover what the user enters. In our case we have data the values ​​of p1 and p2. Which will be the ones we recover.

A second point that we have to pay attention to is the attribute action. In this attribute we have to indicate the name of the file PHP which will process and retrieve the form data. The file that we will code later will be called receive-parameters.php

The last important point of the form will be the type of sending the parameters. The submission type is specified using the method and its values ​​can be POST or GET. With POST the parameters are passed hiddenly, while with GET we can see the values ​​in the request URL. Depending on the shipping method we use, we will need to use one method or another in our code PHP.

Let’s move on to encoding the file PHP. This one will be simple. The method to retrieve data from a form, when we are passing the parameters through the GET method, is $_GET[]. The name of the data to be recovered will be passed as a parameter.

$_GET["p1"];
$_GET["p2"];

Now we will only have to show it on the screen. To do this we use the echo statement in the following way:

echo "The value of p1 is ", $_GET["p1"], "
";
 echo "The value of p2 is ", $_GET["p2"];

Leave a Comment