Form Handling using PHP

14

Click here to load reader

description

How to get data from the form and manipulate data using PHP

Transcript of Form Handling using PHP

Page 2: Form Handling using PHP

$_GET

$_POST

$_REQUEST

2

Page 3: Form Handling using PHP

HTML forms are used to pass data to a server via different input controls.

All input controls are placed in between <form> and </form>

Syntax

<form action=“PagetoOpen ” method=“ ” >

//input controls placed here

</form>

Method= GET or POST

3

Page 4: Form Handling using PHP

4

Page 5: Form Handling using PHP

5

<form method="post" action="" >

First Name <input name="fname" type="text" /> <br />

Last Name <input name="lname" type="text" /> <br />

<input type="submit" value="Save" />

</form>

Page 6: Form Handling using PHP

The GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.

The GET method is restricted to send upto 1024 characters only.

Never use GET method if you have password or other sensitive information to be sent to the server.

GET can't be used to send binary data, like images or word documents, to the server.

Page 7: Form Handling using PHP

7

POST Method

The POST method does not have any restriction on data size to be sent.

The POST method can be used to send ASCII as well as binary data.

The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using this method your information is secure.

Page 8: Form Handling using PHP

The predefined $_GET Variable use to collect values in a form with method=“get” information sent from a form with the GET method is visible to everyone (it will be displayed in the browser address bar ) and has limits on the amount of information to send.

$_GET Variable to collect form data ( the name of the form field will automatically be the keys in the $_GET array )

$_GET[“name”];

$_GET[“fname”];

$_GET[“age”];

8

Page 9: Form Handling using PHP

The predefined $_POST Variable use to collect values in a form with method=“post” information sent from a form with the POST method is invisible to other and has no limits on the amount of information to send.

Note: there is an 8MB max size for the POST Method , by default (can be changed by setting the post_max_size in the php.ini file )

$_POST Variable to collect form data ( the name of the form field will automatically be the keys in the $_POST array )

$_POST[“name”];

$_POST[“fname”];

$_POST[“age”];

9

Page 10: Form Handling using PHP

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

$_REQUEST[“name”];

$_REQUEST[“fname”];

$_REQUEST[“age”];

10

Page 11: Form Handling using PHP

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.

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.

$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method. 11

Page 12: Form Handling using PHP

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.

Note: GET should NEVER be used for sending passwords or other sensitive information!

12

Page 13: Form Handling using PHP

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.

Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

13

Page 14: Form Handling using PHP

www.w3school.com

http://www.tutorialspoint.com/php/index.htm

http://www.w3schools.com/php/php_ref_string.asp

14