Wednesday 15 August 2018

GET and POST superglobals in PHP with code

Superglobals in PHP are built in variables which are always available in all the scopes.
There are many predefined  variable in PHP  as superglobals which actually mean that these variables are always available wherever you want to use them. You don’t have to use anything special to access GET and POST superglobals in PHP with your functions or methods. There are many super globals available in PHP like:
  • $Global
  • $_SERVER
  • $_GET
  • $_POST
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST

The super globals which we are going to discuss here is GET and POST superglobals. Both of these super globals are basically sued to collect form data in PHP.
$_GET is basically an array of variables passed to the pHP script via the URL parameter where as $_POST is an array of variables which are passed to the PHP script via the HTTP POST method.

When to use GET?

Information that is sent from a form using a GET method is basically visible to everyone . Using GET also has a limit on the amount of data to send. The limitation is about 2000 characters. The only useful feature using GET is that variables or data are displayed in the URL so it is possible to bookmark the page.
GET is always being used for sending non-sensitive data.
Now, consider a form with three fields NAME, USERNAME and PASSWORD and a Button, when the user clicks the button the form will be submitted to viaget.php page where the form field data will be processed before it is being saved in MYSQL database table named user where we have an Id, name,username and password fields as under.
 CREATE TABLE `user` (  
  `ID` int(10) NOT NULL AUTO_INCREMENT,  
  `name` varchar(100) NOT NULL,  
  `username` varchar(100) NOT NULL,  
  `password` varchar(100) NOT NULL,  
  PRIMARY KEY (`ID`)  
 )  
 Index.php file:
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
      <meta charset="UTF-8">  
      <title>Get vs GET</title>  
      <style>  
           #wrapper{  
                width: 960px;  
                margin:20px auto;  
                background-color: #f7a959;  
                padding: 20px 0px;  
                border-radius: 20px;  
                border: 2px solid#999;  
           }  
           form,h2{  
                text-align: center;  
                font-size: 18px;  
           }  
           h2{  
                text-shadow: 1px 1px 1px #999;  
                text-decoration: underline;  
                font-size: 20px;  
           }  
           label{  
                color: #381204;  
           }  
           input[type="text"],input[type="password"],input[type="submit"]{  
                width: 300px;  
                height: 30px;  
                border-radius: 5px;  
                border: none;  
                font-size: 20px;  
           }  
      </style>  
 </head>  
 <body>  
      <div id="wrapper">  
           <h2>SUBMISSION OF FORM USING GET METHOD</h2>  
           <form action="viaget.php" method="GET">  
                <p><label for="name">Please enter your Name:</label></p>  
                <p><input type="text" name="name"></p>  
                <p><label for="username">Please enter your Username:</label></p>  
                <p><input type="text" name="username"></p>  
                <p><label for="password">Please enter your Password:</label></p>  
                <p><input type="password" name="password"></p>  
                <p><input type="submit" name="submit" value="Save Record"></p>  
 </form>  
      </div>       
 </body>  
 </html>  
viaget.php file:
 <?php  
 include('db.php');  
      if(isset($_GET['submit'])){  
           $name=$_GET['name'];            
           $username=$_GET['username'];                 
           $password=$_GET['password'];  
           $query="insert into user (name,username,password) values ('$name','$username','$password')";  
            global $conn;  
           $execute=mysqli_query($conn,$query) or die($conn);  
           if($execute){  
                     echo"Record saved successfully";  
                     echo"<br>";  
                     echo "<a href='index.php'>Home</a>";  
           }       
           else{  
                     echo"<script>alert('Sorry something went wrong');</script>";  
           }       
      }  
 ?>  
db.PHP file:
  <?php  
  $conn= mysqli_connect('localhost','root','','superglobals');  
 ?>  
GET and POST superglobals in PHP with code
Finally when the user clicks on the submit button the record will be send via GET method and record will be saved in database.
If you want to learn CRUD Operation by MySqli procedural way in PHP you can learn in this POST.

Note: GET should NEVER be used for sending passwords or other sensitive information!

When to use POST?

Information that is sent from a form using a POST method is invisible to others because all data is embedded within the body of the HTTP request and also it has no limits on the amount of information to send. One important feature about using POST is that it has an advanced functionality as support for multi-part binary input while uploading files to server.

Index.php file:
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
      <meta charset="UTF-8">  
      <title>Get vs Post</title>  
      <style>  
           #wrapper{  
                width: 960px;  
                margin:20px auto;  
                background-color: #f7a959;  
                padding: 20px 0px;  
                border-radius: 20px;  
                border: 2px solid#999;  
           }  
           form,h2{  
                text-align: center;  
                font-size: 18px;  
           }  
           h2{  
                text-shadow: 1px 1px 1px #999;  
                text-decoration: underline;  
                font-size: 20px;  
           }  
           label{  
                color: #381204;  
           }  
           input[type="text"],input[type="password"],input[type="submit"]{  
                width: 300px;  
                height: 30px;  
                border-radius: 5px;  
                border: none;  
                font-size: 20px;  
           }  
      </style>  
 </head>  
 <body>  
      <div id="wrapper">  
           <h2>SUBMISSION OF FORM USING GET METHOD</h2>  
           <form action="viapost.php" method="POST">  
                <p><label for="name">Please enter your Name:</label></p>  
                <p><input type="text" name="name"></p>  
                <p><label for="username">Please enter your Username:</label></p>  
                <p><input type="text" name="username"></p>  
                <p><label for="password">Please enter your Password:</label></p>  
                <p><input type="password" name="password"></p>  
                <p><input type="submit" name="submit" value="Save Record"></p>  
 </form>  
      </div>       
 </body>  
 </html>  

viapost.php file:
  <?php  
 include('db.php');  
      if(isset($_POST['submit'])){  
           $name=$_POST['name'];            
           $username=$_POST['username'];                 
           $password=$_POST['password'];  
           $query="insert into user (name,username,password) values ('$name','$username','$password')";  
            global $conn;  
           $execute=mysqli_query($conn,$query) or die($conn);  
           if($execute){  
                     echo"Record saved successfully";  
                     echo"<br>";  
                     echo "<a href='index.php'>Home</a>";  
           }       
           else{  
                     echo"<script>alert('Sorry something went wrong');</script>";  
           }       
      }  
 ?>  

Finally when the user clicks on the submit button the record will be send via GET method and record will be saved in database.
GET and POST superglobals in PHP with code
Finally when the user clicks on the submit button the record will be send via GET method and record will be saved in database.

Since, that variables or data are not displayed in the URL so it is not possible to bookmark the page.

MYSQL, PHP

0 comments:

Post a Comment

 

© 2018 Mastering Web Development: HTML, Bootstrap, PHP, ASP.NET & VB.NET Essentials - Designed by Mukund | Privacy Policy | Sitemap

About Me | Contact Me | Write For Us