In this lesson, you will learn how to access and manipulate data submitted by users through forms. Information can be submitted through HTML forms to the same or another web page. To send submitted data through a form, one can use GET and post method to do that in PHP.

GET and Post in PHP

A form is a special kind of web page that interacts with the user and get all the necessary information through HTML form tags.

The Get Method

The Get method sends the user information after appending to the page request. The page and the encoded data is separated by the question mark (?) symbol.

The Get method isn’t used to send secure data like passwords. It is limited and can send up to 1024 characters only.

The request string URL is represented as shown below

Example.com/edit.php?id=10&Online=true

The Get method is used only for sending small text information and can’t be used to upload files or images on the server. PHP will give you $_GET associate arrays to access all the information of the form submitted by using the Get method. As data is part of URL, it remained in the cache, and the whole URL can be bookmarked.

Example of  GET method in PHP

Let’s say you have an HTML form which asks you to enter your name. Now, this information is not a secret one and can be sent using GET method.

Practice.php

<div class="form_box shadow">
   <form class="" action="practice_ac.php" method="GET">
     <h4>THE GET METHOD</h4>
     <p>Please enter your name!</p>
     <input type="text" name="query" value=""><br/>
     <input type="submit" name="submit" value="Submit">
   </form>
 </div>
*{box-sizing:border-box;}
body{font-family: 'Open Sans', sans-serif; color:#333; font-size:14px;   padding:100px;}
.form_box{width:400px; padding:20px; background-color:white;}
input{padding:5px; width:100%; margin:5px 0;}
.warning{color:red; font-size:90%;}
.shadow{
  -webkit-box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);
-moz-box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);
box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);}
.pic{text-align:left; width:33%; float:left;}

GET method example

After you enter your name and hit on submit button, the form will be re-directed to practice_ac.php

GET method URL

As you can see the values of form get appended to the URL after question mark sign as key=value pair.

To access this information, PHP provides you with a global associative array referred to as $_GET.

You can access the user’s name is entered using $_GET[‘query’] To view all information, simply print the $_GET array using print_r($_GET).

The Post Method

The post method is different in terms of how it sends data to the web page at the receiving end. It doesn’t let you see the data at all. The limit being enforced on the data being sent via the post method is described by directives in php.in and not by the post method itself. The most method can be used to send files, images, ASCII, and binary data. The data transmitted by post method goes through the HTTP header, so it is safe to send secure data like passwords, username, etc. The PHP provides global associate array $_POST to access the information submitted by the form using the post method.

To send the data by using the post method in the upper form, change

<form class="" action="practice_ac.php" method="GET">

to

<form class="" action="practice_ac.php" method="POST">

You won’t see any data in the URL of the form after you hit the submit button. You can access the data by using $_POST, e.g., $_POST[‘query’] will return you the user name being entered by the user.

PHP Arrays Tutorial Home PHP File Handling

 

Last modified: April 11, 2019