Use of $_ GET and $_ POST in PHP

$_GET, and $_POST are array variables of PHP which are used to read submitted data by HTML form using the get and post method accordingly. Two main differences exist between these two variables. The values of the $_GET array are visible in the URL after submitting the HTML form, but the values of the $_POST array are not visible. $_GET array is used for working with unsecure data, and $_POST array is used for working with secure and large amounts of data. How these two array variables can be used in PHP to read data from the form has shown in this tutorial.

Example 1: Use of $_GET[] to read data from URL

Create a PHP file with the following script to read the username’s value from the URL address and print the value of the username with the other text. If no value is provided for the username, then the script will print another message.

<?PHP

    //Check the variable is set or not
    if(isset($_GET['username']))
    {
        //Print the values of the variable
        echo "The currently logged in username is <b>". $_GET['username']."</b>";
    }
    else
    {
        //Print the message if the no value is assigned
        echo "No user is logged in now";
    }
?>

Output:

The following output will appear if no URL argument is provided with the name username.

The following output will appear if the username is provided in the URL address like below.

http://localhost/php/getpost.php?username=fahmida

Example 2: Use of $_GET[] to read data from the user

Create a PHP file with the following script to read data from a form by using the $_GET[] array. An HTML form of five fields is defined in the script. The form will be submitted with the get method to read the input values by using $_GETT[]. The fields are firstname, lastname, email, phone and a submit button. When the user clicks on the submit button, then the isset() function will return true, and next, the script will check the values of firstname and lastname are empty or not. If any of the field values are empty, then an error message will be printed. If both firstname and lastname contain values, then all the field values of the form will be printed by using the $_GET[] array.

<?php
//Check the submit button is pressed or not
if(isset($_GET['submit']))
{
    //Check the firstname and lastname
    if($_GET["fname"] == "" || $_GET["lname"] == "" ){
        echo "The firstname or lastname can't be empty";
    }
    else
    {
        //Print the submitted values
        echo "First Name: ". $_GET['fname']."<br/>";
        echo "Last Name: ". $_GET['lname']."<br/>";
        echo "Email: ". $_GET['email']."<br/>";
        echo "Phone: ". $_GET['phone'];
    }
}
else
{
?>
<!-- HTML form to take input from the user -->
<html lang="en">
<head>
    <title>Use of PHP $_GET</title>
</head>
<body>
<form method="get" action="#">
    <table>
    <tr><td>
    <label for="inputName">Enter your first name:</label>
    </td><td>
    <input type="text" name="fname" id="fname"><br/>
    </td></tr><tr><td>
    <label for="inputName">Enter your last name:</label>
    </td><td>
    <input type="text" name="lname" id="lname"><br/>
    </td></tr><tr><td>
    <label for="inputName">Enter your email:</label>
    </td><td>
    <input type="text" name="email" id="email"><br/>
    </td></tr><tr><td>
    <label for="inputName">Enter your phone:</label>
    </td><td>
    <input type="text" name="phone" id="phone"><br/>
    </td></tr><tr><td>
    <input type="submit" name="submit" value="Submit"><br/>
    </td><td></td></tr>
    </table>
</form>
</body>
</html>
<?php
}

?>

Output:

The following output will appear after running the script from the webserver. Here, form fields are filled up with dummy data.

The following output will appear after clicking the submit button. The input values of the form are printed here.

Example 3: Use of $_POST[] to read data from the user

Create a PHP file with the following script to check the use of $_POST[] array for taking data from the user. A user login form is designed in the script to take a username and password from the user. The form will be submitted with the post method to read the input values by using $_POST[]. The PHP script will check the submit button is pressed or not by using the isset() function. This function will return true when the user presses the submit button of the form. Next, it will check the values of the username and pass fields. If the user enters “admin” in the username field and “238967” in the pass field, then the conditional statement will return true and print a success message; otherwise, it will print a failure message.

<?PHP
//Check the submit button is pressed or not
if(isset($_POST['submit']))
{
    //Check the firstname and lastname
    if(trim($_POST["username"]) == "admin" || trim($_POST["pass"]) == "238967" ){
        echo "Authenticated User";
    }
    else
    {
        echo "Invalid user";

    }
}
else
{
?>
<!-- HTML form to take input from the user -->
<html lang="en">
<head>
    <title>Use of PHP $_POST</title>
</head>
<body>
<form method="post" action="#">
    <table>
    <tr><td>
    <label for="inputName">Username:</label>
    </td><td>
    <input type="text" name="uname" id="uname"><br/>
    </td></tr><tr><td>
    <label for="inputName">Password:</label>
    </td><td>
    <input type="password" name="pass" id="pass"><br/>
    </td></tr><tr><td>
    <input type="submit" name="submit" value="Submit"><br/>
    </td><td></td></tr>
    </table>
</form>
</body>
</html>
 
<?php
}

?>

Output:

The following output will appear after running the script from the webserver. Here, form fields are filled up with the valid username and the password.

The following success message will be printed if the user provided a valid username and password. According to the script, the valid username is “admin” and the password is “238967”. If any of the wrong value is submitted in any of the fields, then the error message, “Invalid user,” will be printed.

Conclusion

The use of the form is an essential task of any web application because the user’s data are required in most of the web application. $_GET[] and $_POST[] arrays are very useful PHP variables to read the user’s submitted data through any HTML form. But the form must contain a submit button to submit the user’s inserted data to the server using the form. The use of $_GET[] array to read data from the URL address and form data with the get method, and $_POST[] array to read form data with post method are explained in this tutorial. I hope the use of $_GET[], and $_POST[] will be cleared for the readers after reading this tutorial.



from Linux Hint https://ift.tt/37tQwqC

Post a Comment

0 Comments