-----
1) PHP - A Simple HTML Form
2) What is missing here?
3) GET vs. POST
4) When to use GET?
5) When to use POST?
1) PHP - A Simple HTML Form
1.1) The example below displays a simple HTML form with two input fields and a submit button:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
|
1.2) When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.
1.3) To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
|
1.4) Outcome:
1.5) The same result could also be achieved using the HTTP GET method:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
|
1.6) and "welcome_get.php" looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
|
1.7) Outcome:
2) What is missing here?
2.1) The code above is quite simple.
2.2) However, the most important thing is missing. You need to validate form data to protect your script from malicious code.
3) GET vs. POST
3.1) Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.
3.2) Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
3.3) $_GET is an array of variables passed to the current script via the URL parameters.
3.4) $_POST is an array of variables passed to the current script via the HTTP POST method.
4) When to use GET?
4.1) Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
4.2) GET may be used for sending non-sensitive data.
4.3) Note: GET should NEVER be used for sending passwords or other sensitive information!
5) When to use POST?
5.1) Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.
5.2) Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.
5.3) However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
Adapted from: http://www.w3schools.com/php/php_forms.asp
ReplyDeleteThis is really an awesome article. Thank you for sharing this.It is worth reading for everyone.
Hire PHP framework Developers in India