PHP provides two methods (GET & POST) through which a client (forms) can send information to the server.

Two Methos are given below:

  • GET method
  • POST method

1.  GET method:

 GET is a method used to send data browser to server. It sends data to the server as part of the URL. This data is visible to everyone in the URL's address bar and can be bookmarked and cached.

<form action="get_example.php" method="GET">
    Name: <input type="text" name="name"><br>
    Age: <input type="text" name="age"><br>
    <input type="submit" value="Submit">
</form>

PHP script (get_example.php):

<?php
if (isset($_GET['name']) && isset($_GET['age'])) {
    $name = $_GET['name'];
    $age = $_GET['age'];
    echo "Hello, $name! Your age is $age.";
} else {
    echo "Please provide your name and age.";
}
?>

2. POST Method:

POST is a method used to submit data to be processed to a specified resource. It sends data to the server as part of the body of the HTTP request. This data is not visible in the URL and is more secure than GET.

POST requests are suitable for submitting sensitive data, such as user login credentials or uploading files.

Example:

<form action="post_example.php" method="POST">
    Name: <input type="text" name="name"><br>
    Age: <input type="text" name="age"><br>
    <input type="submit" value="Submit">
</form>

PHP script (post_example.php):

<?php
if (isset($_POST['name']) && isset($_POST['age'])) {
    $name = $_POST['name'];
    $age = $_POST['age'];
    echo "Hello, $name! Your age is $age.";
} else {
    echo "Please provide your name and age.";
}
?>

In both examples, a form is created with fields for name and age. When the form is submitted, the data is sent to the PHP script specified in the action attribute. In the PHP script, the data is accessed using $_GET or $_POST superglobal arrays respectively, depending on the method used.