In this tutorial we'll have a look at how to capture form data upon submisssion using php scripts.
We will cover the following:
- Form Element and its Attributes
- name
- action
- method
- GET Method versus POST method
- $_GET ,$_POST and $_REQUEST variables in php
Form Element and its Attributes
The form element takes three attributes name,action and method.
- name: Specifies the name of the form.
- name='myform'
- action: Specifies the URL of the page to be loaded upon submission of form.
- action='URL'
- method: Specifies whether form data will sent to server using GET or POST method.
- method='GET' or method='POST'
Summing up, a form element with all three attributes is defined as such:
<form name='NameOfForm' method=' GET ' action='sompage.php'>
//
<form>
GET Method versus POST method
A form can be submitted using either GET or POST. Before sending form data to server, the data is encoded using these two methods. The difference between the get and post methods is form data encoding.- GET method involves long URL(see later) and non-ASCII character repertoires.
- GET method is less safe than POST method and should be used only and only if form processing does not have side effects.
- GET method is ideal when data needs to be bookmarked.
- GET method should not be used if sensitive data like passwords are being sent.
- POST method produces shorter URL and can handle both ASCII and binary.
$_GET and $_POST variables in php
- $_GET and $ _POST are associative arrays.
- $_GET is used to retrieve all data sent using get method .
- $_POST is used to retrieve all data sent using post method.
NOTE: $_REQUEST variable is also used to access data sent using both get and post methods.
Example 1 using GET method:
Consider the form below:
The user will input his name and comments through this form. On clicking the submit button, a php script will be executed.
The php script captures and displays the name and comments entered by the user.
HTML SCRIPT:
<html>
<head>
</head>
<body>
<form name='frm' action='myphp.php' method='get' >
<table style='width:100%;'>
<tr> <td>Name: <input type='text' name='txt-name' value=''/></td> </tr>
<tr> <td> Comments: <input type='text' name='txt-cmt' value=''/> </td></tr>
<tr> <td> <input type='submit' name='submit' value='Submit'/> </td> </tr>
</table>
</form>
</body>
</html>
HTML Code explained:
The submit button sends the form data to server.
The data is sent to the page URL(myphp.php) specified in the form action,where some operations are performed with the data.(below)
PHP SCRIPT:
The submit button sends the form data to server.
The data is sent to the page URL(myphp.php) specified in the form action,where some operations are performed with the data.(below)
PHP SCRIPT:
<?php
$name=$_GET['txt-name']; //capture name
$comment=$_GET['txt-cmt']; //capture comment
echo "Name: ".$name; //output name
echo "<br/>";
echo "Comment: ".$comment; //output comment
?>
$name=$_GET['txt-name']; //capture name
$comment=$_GET['txt-cmt']; //capture comment
echo "Name: ".$name; //output name
echo "<br/>";
echo "Comment: ".$comment; //output comment
?>
PHP Code Explained:
The php script uses $_GET associative array to access data(name and comment) which was sent using GET method , stores them in $name and $comment variables respectively and output them.
Note: Pay attention to the URL when get method is used.
It should be something similar to that displayed below:
http://localhost/myphp.php?txt-name=John&txt-cmt=John%27s+Comment&submit=Submit
Example 2 using POST Method:
This time we will try out the post method.Steps:
- Copy and modify the previous html code.
- Change method='get' to method='post' and save it.
- Copy previous PHP script as well.
- Change $name=$_GET['txt-name'] to $name=$_POST['txt-name']and
- $comment=$_GET[' txt-cmt '] $comment=$_POST['txt-cmt'].
- Save and run your script.
- Again, pay attention to the URL[http://localhost/myphp.php].
- This time the url is shorter and form data is not appended to the url, which is why it is a lot more secure than get method.
- Still not clear why? Consider the following scenario:
- Suppose you have a login form where users need to type in password and other sensitive infomation; with get method, form data gets cached and someone could easily access them later on. Yet another risk could be that someone might just glance at the user's screen and see the sensitive data appended to the url).
- Obviously in this case, POST method would be necessary.
Example 3 using $_REQUEST Method:
Steps:
<?php
Conclusion: $_REQUEST can be used for both get or post method.
- Simply modify the php script, instead of $_POST and $_GET from previous examples, use $_REQUEST.(see sample code below)
- Save and run your script.
<?php
$name=$_REQUEST['txt-name']; //capture name
$comment=$_REQUEST['txt-cmt']; //capture comment
echo "Name: ".$name; //output name
echo "<br/>";
echo "Comment: ".$comment; //output comment
?>
Conclusion: $_REQUEST can be used for both get or post method.