Created
November 20, 2019 04:07
-
-
Save abid112/67a1cf487902ce3b339954632de75612 to your computer and use it in GitHub Desktop.
This gist shows how to collect submitted form-data from users by using $_POST and $_GET method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$_GET and $_POST are Superglobal variables in PHP which used to collect data from HTML form and URL. | |
There are two ways the browser(client) can send information to the web server. | |
-The GET Method | |
-The POST Method | |
*********The GET Method********* | |
In PHP, the $_GET variable is used to collect values from HTML forms using method get. | |
Information sent from an HTML form with the GET method is displayed in the browser's address bar, and it has a limit on the amount of information to send. | |
The example below contains an HTML form with two input fields, and a submit button fot GET Methood: | |
----------------------------------------------------------------------------- | |
<html> | |
<body> | |
<form action="registration.php" method="get"> | |
Name: <input type="text" name="name"> | |
Email: <input type="text" name="email"> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> | |
----------------------------------------------------------------------------- | |
When the user clicks on the "Submit button", the URL will be something like this: | |
localhost/registration.php?name=Alex&email=alex%40example.com | |
registration.php looks like this: | |
----------------------------------------------------------------------------- | |
<html> | |
<body> | |
Welcome <?php echo $_GET["name"]; ?>! | |
Your email address is <?php echo $_GET["email"]; ?> | |
</body> | |
</html> | |
Output: | |
Welcome Alex! | |
Your email address is [email protected]. | |
----------------------------------------------------------------------------- | |
*********The POST Method********* | |
In PHP, the $_POST variable is used to collect values from HTML forms using method post. | |
Information sent from a form with the POST method is invisible and has no limits on the amount of information to send. | |
The example below contains an HTML form with two input fields, and a submit button fot POST Methood: | |
----------------------------------------------------------------------------- | |
<html> | |
<body> | |
<form action="registration.php" method="post"> | |
Name: <input type="text" name="name"> | |
Email: <input type="text" name="email"> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> | |
----------------------------------------------------------------------------- | |
When the user clicks on the "Submit button", the URL will be something like this: | |
localhost/registration.php | |
registration.php looks like this: | |
----------------------------------------------------------------------------- | |
<html> | |
<body> | |
Welcome <?php echo $_POST["name"]; ?>! | |
Your email address is <?php echo $_POST["email"]; ?> | |
</body> | |
</html> | |
Output: | |
Welcome Alex! | |
Your email address is [email protected]. | |
----------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment