Get and Post Method in PHP Example

Get and Post Method in PHP Example. A Php program to execute the get and post. Using this program we can get and post-program in PHP. The POST method uses HTTP headers to send data. The data is encoded as specified for the GET method and stored in the QUERY STRING header. The data size that can be sent with the POST method is unrestricted. Both ASCII and binary data can be sent using the POST method.

<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="Execute">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = $_POST['fname'];
if (empty($name))
{
echo "Name is empty";
}
else
{
echo $name;
}
}
?>
</body>
</html>

 

Output:

Name: 

 

Post Method in Php

The HTTP request methods Get and Post are used within the form> element to send form data to the server. The HTTP protocol allows communication between the client and the server, where the client is a browser and the server is a programme operating on the computer system that hosts your website. POST is an HTTP request method that is used by the World Wide Web. By definition, the POST request method requests that a web server accept the data included in the body of the request message, most commonly for storage. In contrast, the HTTP GET request method obtains information from the server.

Form Method in Php

The PHP form action property specifies where the data is routed to be processed. To collect data from PHP forms, the superglobals $_POST and $_GET are needed. The GET method is used for non-sensitive data and enables for page bookmarking. Because it is more secure, the POST method is used for sensitive data.