Course Banner

PHP Loops

We pass data from one page to another using form or url link. We will see how we can submit/pass data from one page to another.


< !DOCTYPE html>
< html>
< head>
< title>PHP Form example< /title>
< /head>
< body>
< form name="frm" action="" method="post">
< p>First Name :
< input name="f_name" type="text" id="f_name" />
< /p>
< p><Last Name :
< input name="l_name" type="text" id="l_name" />
< /p>
< p>
< input type="submit" name="Submit" value="Submit" />
< /p>
< /form>
< /body>
< /html>


If you look at above example carefully, there are few important things in form section.

  • ● The data which you want to submit must be enclosed inside < form > and < / form > tag.
  • ● We must define action. In action, either you can specify another page (which you will have to create) or you can leave blank. If you leave blank that means the action will be on same page.

If you want to catch data in another page then the action will be like this action=”submitted_form.php”

  • ● One more important thing in the form section is, ‘method’. You will have to specify in the method section, through which method you want to submit the data.

There are primarily two methods:

  • ● GET
  • ● POST

Before we ahead lets understand the difference between GET and POST method.

Example get method:

GET method


If you submit the data using GET method, the data will be passed through URL like this.


To start with, I will show you example of GET method. In this section we will see how we can submit the data using GET method and how we can catch the submitted data.


  1. error_reporting(E_ERROR);
  2. if($_GET['Submit']){
  3.       echo"First Name : ". $_GET['f_name']."
  4.       echo"Last Name : ". $_GET['l_name'];
  5. }
  6. ?>
PHP Form example

First Name :

Last Name :

After submitting the data, it will look like this http://localhost/form-example.php?f_name=Rajesh&l_name=Kumar&Submit=Submit Result: Notice above carefully to catch submitted data we have used $_GET global variable of PHP. Also in the form section we have given method=”get”. Note: Generally form data is not passed through get method. Because data submitted through get method is visible in URL which can see anyone. So even your password can see which is not good. Data is submitted generally through get method for search box query, because that does not contain any sensitive data. Also, in our above program, we have used error_reporting(E_ALL). This is an built-in PHP function through which we can suppress warning, notices etc. In our case we have told the code to show errors only and hide notices and warnings. One more thing you may have noticed is that the data is passed as an array format. Key value format. Such as : f_name=”Rajesh”, l_name=”Kumar” etc. POST Method Example: "; print_r($_POST); } ?> POST ARRAY FORMAT

First Name :

Last Name :

Result: If you look at above example, you will notice there are few changes in this. First here is’ post’ method instead of get method. And we have printed POST as an array with PHP array print function called print_r(). When we submit the data, the posted data pass as key and value ‘associative’ array format. From above example, you can see. Now , we will print the posted data value with PHP function $_POST[]; Example: POST ARRAY FORMAT

First Name :

Last Name :

Output: Why we have written if($_POST){ We have written above code because, if we don’t write the code, as soon as the page will load, empty will be printed. if($_POST){ writing this code, we tell the page that until the submit button is pressed(data is posted) don’t run the code inside curly braces.