Referensi para programer

Advertisements

Breaking

Monday 1 May 2017

How to use the $ _POST, $ _GET and $ _REQUEST variables

superglobal
Good morning web developers, now I will give you a little information about superglobal variables, ie $ _POST, $ _GET and $ _REQUEST. Actually there are many others like the picture above, but others will be discussed later.
Okay, before going into the coding example. We must first know what is a global variable.

The SuperGlobals variable is a special variable that resides in PHP language and can be accessed from any PHP page in a single project, without needing to define it first, and to access this variable we also do not need to use global keywords (as global variables in general).


1. create the index.html file that contains
 <html lang="en">
<head>
    <title>Tes POST</title>
</head>

  <body style="text-align: center;">

      <form action="hasil.php" method="post">
        <h2>Login Area</h2>
                <input type="text" name="username"  placeholder="Username" autofocus></br></br>
                <input type="password" name="password" placeholder="Password"></br></br>
            <button type="submit" name="login">Masuk</button>
      </form>
  </body>
</html>


Which should be noted here is, the form is sent using the method post, with the characteristics of <form action = "hasil.php" method = "post"> - method = post, then sent to hasil.php

2. create file hasil.php to accommodate results
<?php
    $nama = $_POST['username'];
    $pass = $_POST['password'];
    echo "Username anda $nama</br>";
    echo "Password anda $pass";
?>

Result after run

   Here the post method sent is contained in the variable $name and $pass, function "echo" to display the results. For $ _GET also apply the same, stay changed in index.html to:
<Form action = "hasil.php" method = "get">

And result.php file

      $name = $ _GET ['username'];
      $pass = $ _GET ['password'];

And for $ _REQUEST just to accommodate the submitted methods in either $ _POST or $ _GET

     $name = $ _REQUEST ['username'];
     $pass = $ _REQUEST ['password'];

No comments:

Post a Comment