POST and GET in PHP

HTML Form Basics

Before getting to POST and GET, one must learn the HTML form elements on which they are applied. Following are the types of various form elements

  1. <input> elements. It can be of various types
    • <input type="text" /> for writing simple text
    • <input type="password" /> for writing passwords
    • <input type="checkbox" /> for checkboxes
    • <input type="radio" /> for radio buttons
    • <input type="submit" value="Submit" /> for submitting the form
  1. <select> menu for providing a list down menu
    <select>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
    </select>
  1. <textarea> for large text
    <textarea rows="5" cols="50"></textarea>

All these form elements are generally included inside the <form> tag. It also requires two necessary attributes – action which is the path or the file where form data will be submitted and method which can be either GET or POST.

<form action="submitForm.php" method="POST">

If the action is left empty then the target file is the same file in which form is created.

POST and GET

Before the browser sends the data to the web server, it encodes the information by grouping all the information by using some symbol (like &) and replaces characters like space with their HTML character code. There are two methods by which the client that is the browser can send the submitted data to web server – POST and GET.

GET method

  • GET method uses a string which has all the appended parameters/names and their corresponding data values. This string is used along with the target file and displayed in the browser’s location box. The target file name and the string are separated by using `?’ symbol. Eg- http://www.example.com/target.php?par1=value1&par2=value2&par3=value3.
  • For storing data, all the form elements should have a name attribute which is the name of the parameter in which the data is stored. This name attribute can be any string. Eg – <input type=“text” name=“inputname” />
  • Sent data can be accessed using $_GET in PHP. $_GET uses the same parameter name which is provided in the name attribute of the input element. Eg- $_GET[‘parameterName’].
  • The following code shows how data can be submitted and accessed using GET. There are two files, getform.php which stores the form and gettarget.php which is the action/target file. Essentials required for using various form elements and processing the data are explained within the code.

Getform.php

<html>
<body style="background-color:#196;">

    <form action="gettarget.php" method="GET" />
        Name : <input type="text" name="name" /><br><br>
        Password : <input type="password" name="pwd" /><br><br>

    <!-- In checkbox, the value attribute is used to provide the actual value or the data which will be finally submitted. In this case, the data is checkvalue and the user sees Checkbox written on the page-->
        Checkbox value : <input type="checkbox" name="check" value="checkvalue" />Checkbox<br><br>

    <!-- Similarly in radio button, the value attribute is used to provide the actual value or the data which will be finally submitted. In this case, the data is radiovalue and the user sees Radio button written on the page-->

        Radio button value : <input type="radio" name="radio" value="radiovalue" /> Radio button<br><br>

        Select Menu<br>
            <select name="menu">
            <!-- All the options have the attribute value used similarly as in checkbox and radio buttons-->
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
            </select><br><br>

        <textarea rows="5" cols="50" name="text"></textarea><br><br>
        <input type="submit" value="Submit" /><br><br>
    </form>
    
</body>
</html>

Gettarget.php

<?php

// echo is used to echo or print text in PHP. It is similar to printf in C or cout in C++
echo "<html><body style='background-color:#E8E8E8;'>";

// isset function checks whether a parameter with the given name exists or not. This checks if the form has been submitted or not. If the form has not been submitted, then these varibles won't exist.

if( isset($_GET['name']) && isset($_GET['pwd']) && isset($_GET['check']) && isset($_GET['radio']) && isset($_GET['menu']) && isset($_GET['text']) ){

//Storing the values accessed using GET in other variables. Variables in PHP are defined by using the $ symbol followed by a keyword. It is not necessary to define the type of variable as it is required in other programming languages like C, C++ or JAVA. For instance, $a is a variable which can be used to store a number, character or string. 

    $name = $_GET['name'];
    $pwd = $_GET['pwd'];
    $check = $_GET['check'];
    $radio = $_GET['radio'];
    $menu = $_GET['menu'];
    $text = $_GET['text'];

//Checking whether data sent is empty or not using empty function. '!' symbol stands for NOT

    if(!empty($name))
        echo "<p style='color:green;'>Name is ".$name."</p>"; //HTML elements can be written inside quotes like plain text
    else
        echo "<p style='color:red;'>Name not provided</p>";

    if(!empty($pwd))
        echo "<p style='color:green;'>Password is ".$pwd."</p>";
    else
        echo "<p style='color:red;'>Password not provided</p>";

//The isset function itself checks whether checkbox is ticked or not. If not ticked, then the if condition is not satisfied.

    echo "<p style='color:green;'>Checkbox is ".$check."</p>";

//The isset function itself checks whether checkbox is ticked or not. If not ticked, then the if condition is not satisfied.

    echo "<p style='color:green;'>Radio button is ".$radio."</p>";

    if(!empty($menu))
        echo "<p style='color:green;'>Option selected is ".$menu."</p>";
    else
        echo "<p style='color:red;'>No option selected</p>";

    if(!empty($text))
        echo "<p style='color:green;'>Text is ".$text."</p>";
    else
        echo "<p style='color:red;'>Text not written</p>";

}

else{
    if(!isset($_GET['check']))
        echo '<p style="color:red;">Checkbox not selected</p>';
    if(!isset($_GET['radio']))
        echo '<p style="color:red;">Radio button not selected</p>';
}

echo "</body></html>";

?>

Observe the URL in this case Minion

Another thing to observe in this code is how HTML code can be written inside PHP code. PHP code simply echoes or prints HTML code that it finds according to the conditions satisfied.

POST method

  • POST method does not use any string like in GET method. Instead the URL remains unchanged and same as it is before submitting the form Eg- http://www.example.com/target.php.
  • For storing data, all the form elements should have a name attribute which is the name of the parameter in which the data is stored. This name attribute can be any string. Eg – <input type=“text” name=“inputname” />
  • Sent data can be accessed using $_POST in PHP. $_POST uses the same parameter name which is provided in the name attribute of the input element. Eg- $_GET[‘parameterName’].
  • The following code shows how data can be submitted and accessed using POST. There are two files, postform.php which stores the form and posttarget.php which is the action/target file. Essentials required for using various form elements and processing the data are explained within the code.

Postform.php

<html>
<body style="background-color:#196;">

    <form action="posttarget.php" method="POST" />
        Name : <input type="text" name="name" /><br><br>
        Password : <input type="password" name="pwd" /><br><br>
        Checkbox value : <input type="checkbox" name="check" value="checkvalue" /> Checkbox<br><br>
        Radio button value : <input type="radio" name="radio" value="radiovalue" /> Radio button<br><br>
        
        Select Menu<br>
            <select name="menu">
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
            </select><br><br>

        <textarea rows="5" cols="50" name="text"></textarea><br><br>
        <input type="submit" value="Submit" /><br><br>
    </form>

</body>
</html>

Posttarget.php

<?php
    echo "<html><body style='background-color:#E8E8E8;'>";

// isset function checks whether a parameter with the given name exists or not

if( isset($_POST['name']) && isset($_POST['pwd']) && isset($_POST['check']) && isset($_POST['radio']) && isset($_POST['menu']) && isset($_POST['text']) ){

//Storing the values accessed using POST in other variables
    $name = $_POST['name'];
    $pwd = $_POST['pwd'];
    $check = $_POST['check'];
    $radio = $_POST['radio'];
    $menu = $_POST['menu'];
    $text = $_POST['text'];

//Checking whether data sent is empty or not using empty function. '!' symbol stand for NOT

    if(!empty($name))
        echo "<p style='color:green;'>Name is ".$name."</p>"; //HTML elements can be written inside quotes like plain text
    else
        echo "<p style='color:red;'>Name not provided</p>";

    if(!empty($pwd))
        echo "<p style='color:green;'>Password is ".$pwd."</p>";
    else
        echo "<p style='color:red;'>Password not provided</p>";

//The isset function itself checks whether checkbox is ticked or not. If not ticked, then the if condition is not satisfied.

    echo "<p style='color:green;'>Checkbox is ".$check."</p>";

//The isset function itself checks whether checkbox is ticked or not. If not ticked, then the if condition is not satisfied.

    echo "<p style='color:green;'>Radio button is ".$radio."</p>";

    if(!empty($menu))
        echo "<p style='color:green;'>Option selected is ".$menu."</p>";
    else
        echo "<p style='color:red;'>No option selected</p>";

    if(!empty($text))
        echo "<p style='color:green;'>Text is ".$text."</p>";
    else
        echo "<p style='color:red;'>Text not written</p>";

}

else{
    if(!isset($_POST['check']))
        echo '<p style="color:red;">Checkbox not selected</p>';
    if(!isset($_POST['radio']))
        echo '<p style="color:red;">Radio button not selected</p>';
}

echo "</body></html>";

?>

Observe the URL in this case. As you can see, no information is appended with the target file name in the URL.

Minion

This content has been helpful to you?

Thanks for contributing!

Yes No