$_REQUEST and $_GET Part-1



PHP deals with html, css and js. For this reason, it is possible to control .js, .css.html file by using php. Anywhere of html, php code can be used. If we want to take value or data from a form and send it to a server then php gives solution. We have designed this post into different parts. In this part, we will discuss about form elements of html, its relation with php and also usage in real life.  

Form Elements

A form is consisted with input(to give value such as text, date, number, date etc.), submission button. Inside <form> tag there are two attributes such as action and method. This two attributes are so much important for getting data and send it to server. action fixes that where form will go after clicking send/submit button and method is way of sending data by in hidden way or publicly. Let's see an example below  


<!DOCTYPE html>
<html lang="en">
    <head>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/font-awesome.min.css" rel="stylesheet">
        <style>
            form{max-width: 600px; margin: 50px auto; }
        </style>
    </head>
    <body>
        
<form method="GET" action="<?php $_SERVER['PHP_SELF']; ?>" class="form-group" > <input name="username" type="text" class="form-control">
<input type="submit" value="Submit" class="form-control btn btn-success"> </form>
</body> </html>

  If we notice then we see that inside <form> tag there are two attributes action and method. The value of method is GET and value of action is $_SERVER['PHP_SELF']. You can also use file name in lieu of  $_SERVER['PHP_SELF']. If you keep action blank then action will occur in current page. GET is usually used for sending data publicly. After making a form by above code, we get a form like below. In our  form there are two inputs. One for giving value and other is for submission. When a user will click the submit button, action will be occurred in this page. We have used GET in method attribute. During sending data address will be changed like this. As we are working with index.php. So data will be sent with the help of name attribute of first input. We are giving value John in text box. So let's see the picture below.   Noticing at address bar we see that given data is appened with our file name with the help of name attributes after question(?) sign. As we used username as attributes. So John is assigned in username. Finally address has become /index.php?username=John If we want to send more data by using several name attributes then every name attributes will be added with each other by & sign. Let's see the below example /index.php?username=John&pass=123&address=CA&mobile=01-123xxxxx GET is not a secure way of sending data. Because every data received a form is shown in address bar which creates security vulnerability. But is better for bookmarking and showing page number or something like that.  

Using $_REQUEST

As we have sent data by using form. Now it is our duty to catch data. For doing that $_REQUEST will help us. We will first check by using if condition whether  the form's action is GET or POST. Let's see below code.


<!DOCTYPE html>
<html lang="en">
    <head>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/font-awesome.min.css" rel="stylesheet">
        <style> 
            body{
                background: #ddd;
            }
            form{max-width: 600px; margin: 50px auto; }
        </style>

    </head>
    <body>
        
<?php $name = ''; if ($_SERVER['REQUEST_METHOD'] == 'GET') { $name = $_GET['username']; } ?> <form class="form-group" method="get" action="<?php $_SERVER['PHP_SELF']; ?>"> <input type="text" name="username" class="form-control">
<input type="submit" value="Submit" class="form-control btn btn-success"> <?php echo $name; ?> </form>
</body> </html>

We will see John as output in the browser. Hope that you have got a clear idea about form, its usage and GET, REQUEST_METHOD. In next post we will discuss about post and its usage.