PHP GET AND POST
Get and Post Methods in PHP
- PHP provides two methods through which a client (browser) can send information to the server. These methods are given below, and discussed in detail:
- GET method
- POST method
Get and Post Methods in PHP(contd.)
- Get and Post methods are the HTTP request methods used inside the <form> tag to send form data to the server.
- HTTP protocol enables the communication between the client and the server where a browser can be the client, and an application running on a computer system that hosts your website can be the server.
GET
- The GET method is used to submit the HTML form data. This data is collected by the predefined $_GET variable for processing.
- The information sent from an HTML form using the GET method is visible to everyone in the browser’s address bar, which means that all the variable names and their values will be displayed in the URL. Therefore, the get method is not secured to send sensitive information.
POST
- The POST method is also used to submit the HTML form data. But the data submitted by this method is collected by the predefined superglobal variable $_POST instead of $_GET.
- Unlike the GET method, it does not have a limit on the amount of information to be sent. The information sent from an HTML form using the POST method is not visible to anyone.
$_REQUEST variable
- The $_REQUEST variable is a superglobal variable, which can hold the content of both $_GET and $_POST variable.
- In other words, the PHP $_REQUEST variable is used to collect the form data sent by either GET or POST methods.
Example 1
Create a file get.php
<html>
<body>
<form action = “gettest.php” method = “GET”>
<br>
<br>
Username: <input type = “text” name = “username” /> Blood Group: <input type = “text” name = “bloodgroup” />
<input type = “submit” />
</form>
</body>
</html>
Create a file gettest.php
<html>
<body>
Welcome <?php echo $_GET[“username”]; ?> </br>
Your blood group is: <?php echo $_GET[“bloodgroup”];
?>
</body>
</html>
Execute get.php OUTPUT:
Welcome Navneet Your blood group is: A+
Example 2
Create a file post.php
<html>
<body>
<form action = “posttest.php” method = “post”>
Username: <input type = “text” name = “username” /> <br>
Blood Group: <input type = “text” name = “bloodgroup” />
<br>
<input type = “submit” />
</form>
</body>
</html>
Create a file posttest.php
<html>
<body>
Welcome <?php echo $_POST[“username”]; ?> </br>
Your blood group is: <?php echo
$_POST[“bloodgroup”]; ?>
</body>
</html>
Execute post.php OUTPUT:
Welcome Navneet Your blood group is: A+
Example 3
Create a file get-method.php
<!DOCTYPE html>
<html lang=”en”>
<head>
<title>Example of PHP GET method</title>
</head>
<body>
<?php if(isset($_GET[“name”])){
echo “<p>Hi, ” . $_GET[“name”] . “</p>”;
}
?>
<form method=”get” action=”<?php echo $_SERVER[“PHP_SELF”];?>”>
<label for=”inputName”>Name:</label>
<input type=”text” name=”name” id=”inputName”>
<input type=”submit” value=”Submit”>
</form>
</body>
Execute get-method.php OUTPUT:
Hi, navneet kaur
Example 4
Create a file post-method.php
<!DOCTYPE html>
<html lang=”en”>
<head>
<title>Example of PHP POST method</title>
</head>
<body>
<?php if(isset($_POST[“name”])){
echo “<p>Hi, ” . $_POST[“name”] . “</p>”;
}
?>
<form method=”post” action=”<?php echo $_SERVER[“PHP_SELF”];?>”>
<label for=”inputName”>Name:</label>
<input type=”text” name=”name” id=”inputName”>
<input type=”submit” value=”Submit”>
</form>
</body>
Execute post-method.php OUTPUT:
Hi, navneet kaur
[pdf_note link=”https://drive.google.com/drive/folders/1E7AFJli0W5bH6_SBJTcVANNKzuMTNoJ_”]