Form handling is one of the common tasks when creating a project in PHP. There are various methods are available for this and they are good according to the situation.
You can either pass the form data by URL and hidden them from the user.
Contents
1. $_GET
- 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.
- $_GET may be used for sending non-sensitive data. For example, fetch a record by parameter passed in the URL.
Example
<?php if(isset($_GET['submit'])){ Â Â $name = $_GET['name']; Â Â $email = $_GET['email']; Â Â $age = $_GET['age']; Â Â echo "name : ".$name."<br>"; Â Â echo "email : ".$email."<br>"; Â Â echo "age : ".$age; } ?> <form method='get' action=''> Â Â Name : <input type='text' name='name' /> <br/> Â Â Email : <input type='text' name='email' /> <br/> Â Â Age : <input type='text' name='age' /> <br/> Â Â <input type='submit' value='Submit' name='submit'> </form>
2. $_POST
- Information sent from a
<form >
with POST method is invisible to others and has no limits on the amount of information to send. - Moreover, $_POST support advanced functionality such as support for multipart binary input while uploading files to the server.
- However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
Example
<?php if(isset($_POST['submit'])){ Â Â $name = $_POST['name']; Â Â $email = $_POST['email']; Â Â $age = $_POST['age']; Â Â echo "name : ".$name."<br>"; Â Â echo "email : ".$email."<br>"; Â Â echo "age : ".$age; } ?> <form method='post' action=''> Â Â Name : <input type='text' name='name' /> <br/> Â Â Email : <input type='text' name='email' /> <br/> Â Â Age : <input type='text' name='age' /> <br/> Â Â <input type='submit' value='Submit' name='submit'> </form>
3. $_REQUEST
- This method work as GET method if it is used in
<form method='' >
. - It contains the contents of $_GET, $_POST, and $_COOKIE.
Example
<?php if(isset($_REQUEST['submit'])){ Â Â $name = $_REQUEST['name']; Â Â $email = $_REQUEST['email']; Â Â $age = $_REQUEST['age']; Â Â echo "name : ".$name."<br>"; Â Â echo "email : ".$email."<br>"; Â Â echo "age : ".$age; } ?> <form method='request' action=''> Â Â Name : <input type='text' name='name' /> <br/> Â Â Email : <input type='text' name='email' /> <br/> Â Â Age : <input type='text' name='age' /> <br/> Â Â <input type='submit' value='Submit' name='submit'> </form>
4. Conclusion
Use methods according to your requirement within the <form >
. With $_REQUEST method you can either read POST or GET method data but avoid it because this should open security holes in your website.