PHP $_GET Predefined Variable
The $_GET predefined variable contains an associative array of variables passed to the current page via the URL parameters (querystring). When using the $_GET superglobal variable, the HTML <form> tag must have the attribute method=”get”. It is identical to the $_POST predefined variable except the data are visible in the URL.
Syntax
$variablename = $_GET['HTML CONTROL'];
Create a new HTML page, name it get.html and copy this:
<form action="get.php" method="get"> Name: <input type="text" name="fullname"/> Email: <input type="email" name="email"/> Comment:<textarea name="comment"> </textarea> <input type="submit"/> </form>
Now make a PHP page called get.php in the same directory, and then copy this:
<?php $fullname = $_GET['fullname']; $email = $_GET['email']; $comment = $_GET['comment']; echo "Your name is <b>$fullname</b> and your email is <b>$email</b> we will not store your email."; echo "<br/> Thanks for your comment: <b>$comment</b>"; echo "<br/> Look how the URL passes the data!" ?>
Run the HTML page and fill in the details, and you should see your results.
Code Explained
- As in the POST tutorial, you make the variables and get the HTML controls
- Then you echo out the results