PHP Forms
As you would have noticed in the earlier posts,the $_GET and $_POST variables are used to retrieve information from a web form.All HTML form elements are automatically available to the PHP script.Lets see how the form elements are made available to the PHP script.
<html>
<body><form action=”welcome.php” method=”post”>
Name: <input type=”text” name=”username” />
<input type=”submit” />
</form></body>
</html>
Now when the submit button is clicked,the “welcome.php” script is called,which looks something like this
<html>
<body>Welcome <?php echo $_POST["username"]; ?>!
</body>
</html>
The above form passes values using the “post” method and hence the $_POST function is used to retrieve the form data.
The $_GET environment variable is used to retrieve data from forms with the “get” method.The main difference between the $_GET and $_POST is that the information that is passed from the form is visible to everyone,(it will be visible in the browser) and also there is a space limitation on the amount of information that can be send.
Lets see the above example using the $_GET function
<html>
<body><form action=”welcome.php” method=”get”>
Name: <input type=”text” name=”username” />
<input type=”submit” />
</form></body>
</html>
Here to retrieve the value,we use the $_GET function in the welcome.php script
<html>
<body>Welcome <?php echo $_GET["username"]; ?>!
</body>
</html>
The difference between the $_GET and $_POST functions can be noticed in the URLs of the two.
As such,we should not use the $_GET function to pass sensitive data like password or credit card numbers.Also the amount of information that can be passed should not exceed 1024 characters.The variables are displayed in the URL and hence $_GET allows bookmarking of the web pages.Also,we cannot send binary data like images to the web server using get method.
When using $_POST,there is no limit in the amount of information that can be passed and also the data is invisible.But,since the variables are not available with the URL,we cannot bookmark the web page.
The $_REQUEST function can be used to collect information sent with both get or post methods.
<html>
<body>Welcome <?php echo $_REQUEST["username"]; ?>!
</body>
</html>
Related posts:








Leave your response!